dovecot-2.0: Use net_connect_unix_with_retries() instead of dupl...

dovecot at dovecot.org dovecot at dovecot.org
Fri Oct 23 05:25:37 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/e027503ddb6b
changeset: 10159:e027503ddb6b
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Oct 22 22:25:31 2009 -0400
description:
Use net_connect_unix_with_retries() instead of duplicating the code everywhere.

diffstat:

6 files changed, 18 insertions(+), 53 deletions(-)
src/auth/auth-worker-server.c            |   32 +++++-------------------------
src/lib-auth/auth-master.c               |   11 +---------
src/lib-auth/auth-server-connection.c    |   12 ++---------
src/lib-master/master-login-auth.c       |    2 -
src/lib-master/master-service-settings.c |    2 -
src/login-common/main.c                  |   12 ++++-------

diffs (152 lines):

diff -r 1bc88aa1373f -r e027503ddb6b src/auth/auth-worker-server.c
--- 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 *au
 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 @@ static void auth_worker_destroy(struct a
 
 	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);
 	}
 }
 
diff -r 1bc88aa1373f -r e027503ddb6b src/lib-auth/auth-master.c
--- 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 void auth_input(struct auth_maste
 
 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",
diff -r 1bc88aa1373f -r e027503ddb6b src/lib-auth/auth-server-connection.c
--- 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(struc
 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",
diff -r 1bc88aa1373f -r e027503ddb6b src/lib-master/master-login-auth.c
--- 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 @@ master_login_auth_connect(struct master_
 
 	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);
diff -r 1bc88aa1373f -r e027503ddb6b src/lib-master/master-service-settings.c
--- 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 @@ master_service_read_config(struct master
 		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);
diff -r 1bc88aa1373f -r e027503ddb6b src/login-common/main.c
--- 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)
 static int anvil_connect(void)
 {
 #define ANVIL_HANDSHAKE "VERSION\tanvil\t1\t0\n"
-	int i = 0, fd;
-
-	while ((fd = net_connect_unix("anvil")) == -1) {
-		if (errno != EAGAIN || ++i == 3)
-			i_fatal("net_connect_unix(anvil) failed: %m");
-		sleep(1);
-	}
+	int fd;
+
+	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)


More information about the dovecot-cvs mailing list