dovecot-2.0: director: If connecting to director fails, try conn...

dovecot at dovecot.org dovecot at dovecot.org
Wed May 19 14:08:37 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/9d886ae434c3
changeset: 11327:9d886ae434c3
user:      Timo Sirainen <tss at iki.fi>
date:      Wed May 19 13:08:33 2010 +0200
description:
director: If connecting to director fails, try connecting to next one.

diffstat:

 src/director/director-connection.c |  20 ++++++++++++++++----
 src/director/director-host.h       |   3 +++
 src/director/director.c            |  12 ++++++++++--
 3 files changed, 29 insertions(+), 6 deletions(-)

diffs (114 lines):

diff -r fc94106ca7e9 -r 9d886ae434c3 src/director/director-connection.c
--- a/src/director/director-connection.c	Wed May 19 12:45:54 2010 +0200
+++ b/src/director/director-connection.c	Wed May 19 13:08:33 2010 +0200
@@ -370,7 +370,7 @@
 		director_handshake_cmd_done(conn);
 		return TRUE;
 	}
-	i_error("director(%s): Unknown command (in this state): %s",
+	i_error("director(%s): Invalid handshake command: %s",
 		conn->name, cmd);
 	return FALSE;
 }
@@ -456,8 +456,15 @@
 		i_error("director(%s): Received empty line", conn->name);
 		return FALSE;
 	}
-	if (!conn->handshake_received)
-		return director_connection_handle_handshake(conn, cmd, args);
+	if (!conn->handshake_received) {
+		if (!director_connection_handle_handshake(conn, cmd, args)) {
+			/* invalid commands during handshake,
+			   we probably don't want to reconnect here */
+			conn->host->last_failed = ioloop_time;
+			return FALSE;
+		}
+		return TRUE;
+	}
 
 	if (strcmp(cmd, "USER") == 0)
 		return director_cmd_user(conn, args);
@@ -623,13 +630,18 @@
 
 static void director_connection_connected(struct director_connection *conn)
 {
+	struct director *dir = conn->dir;
 	string_t *str = t_str_new(1024);
 	int err;
 
 	if ((err = net_geterror(conn->fd)) != 0) {
+		conn->host->last_failed = ioloop_time;
 		i_error("director(%s): connect() failed: %s", conn->name,
 			strerror(err));
 		director_connection_deinit(&conn);
+
+		/* try connecting to next server */
+		director_connect(dir);
 		return;
 	}
 	conn->connected = TRUE;
@@ -641,7 +653,7 @@
 	director_connection_send_hosts(str);
 	director_connection_send(conn, str_c(str));
 
-	conn->user_iter = user_directory_iter_init(conn->dir->users);
+	conn->user_iter = user_directory_iter_init(dir->users);
 	(void)director_connection_send_users(conn);
 }
 
diff -r fc94106ca7e9 -r 9d886ae434c3 src/director/director-host.h
--- a/src/director/director-host.h	Wed May 19 12:45:54 2010 +0200
+++ b/src/director/director-host.h	Wed May 19 13:08:33 2010 +0200
@@ -17,6 +17,9 @@
 	   trust the one that has the highest sequence. */
 	unsigned int last_seq;
 
+	/* Last time host was detected to be down/broken */
+	time_t last_failed;
+
 	/* we are this director */
 	unsigned int self:1;
 };
diff -r fc94106ca7e9 -r 9d886ae434c3 src/director/director.c
--- a/src/director/director.c	Wed May 19 12:45:54 2010 +0200
+++ b/src/director/director.c	Wed May 19 13:08:33 2010 +0200
@@ -10,6 +10,8 @@
 #include "director-connection.h"
 #include "director.h"
 
+#define DIRECTOR_RECONNECT_RETRY_SECS 60
+
 static bool director_is_self_ip_set(struct director *dir)
 {
 	struct ip_addr ip;
@@ -90,14 +92,20 @@
 
 	i_assert(dir->right == NULL);
 
+	if (host->last_failed + DIRECTOR_RECONNECT_RETRY_SECS > ioloop_time) {
+		/* failed recently, don't try retrying here */
+		return -1;
+	}
+
 	fd = net_connect_ip(&host->ip, host->port, NULL);
 	if (fd == -1) {
+		host->last_failed = ioloop_time;
 		i_error("connect(%s) failed: %m", host->name);
 		return -1;
 	}
 
 	dir->right = director_connection_init_out(dir, fd, host);
-	return 1;
+	return 0;
 }
 
 void director_connect(struct director *dir)
@@ -114,7 +122,7 @@
 	for (i = 1; i < count; i++) {
 		unsigned int idx = (self_idx + i) % count;
 
-		if (director_connect_host(dir, hosts[idx]) > 0)
+		if (director_connect_host(dir, hosts[idx]) == 0)
 			break;
 	}
 	if (i == count) {


More information about the dovecot-cvs mailing list