changeset 10159:e027503ddb6b HEAD

Use net_connect_unix_with_retries() instead of duplicating the code everywhere.
author Timo Sirainen <tss@iki.fi>
date Thu, 22 Oct 2009 22:25:31 -0400
parents 1bc88aa1373f
children e519b53b3f87
files src/auth/auth-worker-server.c src/lib-auth/auth-master.c src/lib-auth/auth-server-connection.c src/lib-master/master-login-auth.c src/lib-master/master-service-settings.c src/login-common/main.c
diffstat 6 files changed, 17 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/auth/auth-worker-server.c	Thu Oct 22 22:25:08 2009 -0400
+++ b/src/auth/auth-worker-server.c	Thu Oct 22 22:25:31 2009 -0400
@@ -123,34 +123,15 @@
 static struct auth_worker_connection *auth_worker_create(struct auth *auth)
 {
 	struct auth_worker_connection *conn;
-	int fd, try;
+	int fd;
 
 	if (array_count(&connections) >= auth->set->worker_max_count)
 		return NULL;
 
-	for (try = 0;; try++) {
-		fd = net_connect_unix(worker_socket_path);
-		if (fd >= 0)
-			break;
-
-		if (errno == EAGAIN || errno == ECONNREFUSED) {
-			/* we're busy. */
-		} else if (errno == ENOENT) {
-			/* master didn't yet create it? */
-		} else {
-			i_fatal("net_connect_unix(%s) failed: %m",
-				worker_socket_path);
-		}
-
-		if (try == 50) {
-			i_error("net_connect_unix(%s) "
-				"failed after %d secs: %m",
-				worker_socket_path, try/10);
-			return NULL;
-		}
-
-		/* wait and try again */
-		usleep(100000);
+	fd = net_connect_unix_with_retries(worker_socket_path, 5000);
+	if (fd == -1) {
+		i_fatal("net_connect_unix(%s) failed: %m",
+			worker_socket_path);
 	}
 
 	conn = i_new(struct auth_worker_connection, 1);
@@ -208,8 +189,7 @@
 
 	if (idle_count == 0 && restart) {
 		conn = auth_worker_create(auth);
-		if (conn != NULL)
-			auth_worker_request_send_next(conn);
+		auth_worker_request_send_next(conn);
 	}
 }
 
--- a/src/lib-auth/auth-master.c	Thu Oct 22 22:25:08 2009 -0400
+++ b/src/lib-auth/auth-master.c	Thu Oct 22 22:25:31 2009 -0400
@@ -242,19 +242,12 @@
 
 static int auth_master_connect(struct auth_master_connection *conn)
 {
-	int fd, try;
+	int fd;
 
 	i_assert(conn->fd == -1);
 
 	/* max. 1 second wait here. */
-	for (try = 0; try < 10; try++) {
-		fd = net_connect_unix(conn->auth_socket_path);
-		if (fd != -1 || (errno != EAGAIN && errno != ECONNREFUSED))
-			break;
-
-		/* busy. wait for a while. */
-		usleep(((rand() % 10) + 1) * 10000);
-	}
+	fd = net_connect_unix_with_retries(conn->auth_socket_path, 1000);
 	if (fd == -1) {
 		if (errno == EACCES) {
 			i_error("userdb lookup: %s",
--- a/src/lib-auth/auth-server-connection.c	Thu Oct 22 22:25:08 2009 -0400
+++ b/src/lib-auth/auth-server-connection.c	Thu Oct 22 22:25:31 2009 -0400
@@ -377,21 +377,15 @@
 int auth_server_connection_connect(struct auth_server_connection *conn)
 {
 	const char *handshake;
-	int fd, try;
+	int fd;
 
 	i_assert(conn->fd == -1);
 
 	conn->last_connect = ioloop_time;
 
 	/* max. 1 second wait here. */
-	for (try = 0; try < 10; try++) {
-		fd = net_connect_unix(conn->client->auth_socket_path);
-		if (fd != -1 || (errno != EAGAIN && errno != ECONNREFUSED))
-			break;
-
-		/* busy. wait for a while. */
-		usleep(((rand() % 10) + 1) * 10000);
-	}
+	fd = net_connect_unix_with_retries(conn->client->auth_socket_path,
+					   1000);
 	if (fd == -1) {
 		if (errno == EACCES) {
 			i_error("auth: %s",
--- a/src/lib-master/master-login-auth.c	Thu Oct 22 22:25:08 2009 -0400
+++ b/src/lib-master/master-login-auth.c	Thu Oct 22 22:25:31 2009 -0400
@@ -240,7 +240,7 @@
 
 	i_assert(auth->fd == -1);
 
-	fd = net_connect_unix(auth->auth_socket_path);
+	fd = net_connect_unix_with_retries(auth->auth_socket_path, 1000);
 	if (fd == -1) {
 		i_error("net_connect_unix(%s) failed: %m",
 			auth->auth_socket_path);
--- a/src/lib-master/master-service-settings.c	Thu Oct 22 22:25:08 2009 -0400
+++ b/src/lib-master/master-service-settings.c	Thu Oct 22 22:25:31 2009 -0400
@@ -111,7 +111,7 @@
 		fd = service->config_fd;
 		service->config_fd = -1;
 	} else {
-		fd = net_connect_unix(path);
+		fd = net_connect_unix_with_retries(path, 1000);
 		if (fd < 0) {
 			*error_r = t_strdup_printf(
 				"net_connect_unix(%s) failed: %m", path);
--- a/src/login-common/main.c	Thu Oct 22 22:25:08 2009 -0400
+++ b/src/login-common/main.c	Thu Oct 22 22:25:31 2009 -0400
@@ -78,13 +78,11 @@
 static int anvil_connect(void)
 {
 #define ANVIL_HANDSHAKE "VERSION\tanvil\t1\t0\n"
-	int i = 0, fd;
+	int fd;
 
-	while ((fd = net_connect_unix("anvil")) == -1) {
-		if (errno != EAGAIN || ++i == 3)
-			i_fatal("net_connect_unix(anvil) failed: %m");
-		sleep(1);
-	}
+	fd = net_connect_unix_with_retries("anvil", 5000);
+	if (fd == -1)
+		i_fatal("net_connect_unix(anvil) failed: %m");
 	net_set_nonblock(fd, FALSE);
 
 	if (write(fd, ANVIL_HANDSHAKE, strlen(ANVIL_HANDSHAKE)) < 0)