From 8be725bde61579a5117a6be9ff51cbfadde03403 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 25 Apr 2014 19:32:24 +1000 Subject: [PATCH] Retry binding the connector to the socket for up to 2 minutes --- src/connector.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/connector.c b/src/connector.c index 2da6450d..085723c5 100644 --- a/src/connector.c +++ b/src/connector.c @@ -329,6 +329,7 @@ int connector(proc_instance_t *pi) ckpool_t *ckp = pi->ckp; int sockd, ret = 0; conn_instance_t ci; + int tries = 0; if (ckp->serverurl) { if (!extract_sockaddr(ckp->serverurl, &url, &port)) { @@ -336,11 +337,18 @@ int connector(proc_instance_t *pi) ret = 1; goto out; } - sockd = bind_socket(url, port); + do { + sockd = bind_socket(url, port); + if (sockd > 0) + break; + LOGWARNING("Connector failed to bind to socket, retrying in 5s"); + sleep(5); + } while (++tries < 25); + dealloc(url); dealloc(port); if (sockd < 0) { - LOGERR("Connector failed to bind to socket"); + LOGERR("Connector failed to bind to socket for 2 minutes"); ret = 1; goto out; } @@ -357,13 +365,21 @@ int connector(proc_instance_t *pi) serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(3333); - ret = bind(sockd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); + do { + ret = bind(sockd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); + if (!ret) + break; + LOGWARNING("Connector failed to bind to socket, retrying in 5s"); + sleep(5); + } while (++tries < 25); if (ret < 0) { - LOGERR("Connector failed to bind to socket"); + LOGERR("Connector failed to bind to socket for 2 minutes"); close(sockd); goto out; } } + if (tries) + LOGWARNING("Connector successfully bound to socket"); ret = listen(sockd, 10); if (ret < 0) {