dovecot-2.0: director: Code cleanup - keep mail hosts in a struc...

dovecot at dovecot.org dovecot at dovecot.org
Thu May 20 13:24:00 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/19336bddada2
changeset: 11352:19336bddada2
user:      Timo Sirainen <tss at iki.fi>
date:      Thu May 20 12:23:55 2010 +0200
description:
director: Code cleanup - keep mail hosts in a struct rather than in static variables.

diffstat:

 src/director/director-connection.c |   14 ++--
 src/director/director-request.c    |    3 +-
 src/director/director.c            |    4 +-
 src/director/director.h            |    1 +
 src/director/doveadm-connection.c  |   13 ++--
 src/director/mail-host.c           |  115 +++++++++++++++++++++----------------
 src/director/mail-host.h           |   25 +++++---
 src/director/main.c                |   10 +--
 8 files changed, 105 insertions(+), 80 deletions(-)

diffs (truncated from 477 to 300 lines):

diff -r c57c1ff9c5f1 -r 19336bddada2 src/director/director-connection.c
--- a/src/director/director-connection.c	Thu May 20 11:46:21 2010 +0200
+++ b/src/director/director-connection.c	Thu May 20 12:23:55 2010 +0200
@@ -199,7 +199,7 @@
 		return FALSE;
 	}
 
-	host = mail_host_lookup(&ip);
+	host = mail_host_lookup(conn->dir->mail_hosts, &ip);
 	if (host == NULL) {
 		i_error("director(%s): USER used unknown host %s in handshake",
 			conn->name, args[1]);
@@ -249,7 +249,7 @@
 
 	if (remote_ring_completed && !conn->dir->ring_handshaked) {
 		/* clear everything we have and use only what remote sends us */
-		hosts = mail_hosts_get();
+		hosts = mail_hosts_get(conn->dir->mail_hosts);
 		while (array_count(hosts) > 0) {
 			hostp = array_idx(hosts, 0);
 			director_remove_host(conn->dir, conn->host, *hostp);
@@ -281,9 +281,9 @@
 		return TRUE;
 	}
 
-	host = mail_host_lookup(&ip);
+	host = mail_host_lookup(conn->dir->mail_hosts, &ip);
 	if (host == NULL) {
-		host = mail_host_add_ip(&ip);
+		host = mail_host_add_ip(conn->dir->mail_hosts, &ip);
 		update = TRUE;
 	} else {
 		update = host->vhost_count != vhost_count;
@@ -311,7 +311,7 @@
 		return FALSE;
 	}
 
-	host = mail_host_lookup(&ip);
+	host = mail_host_lookup(conn->dir->mail_hosts, &ip);
 	if (host != NULL)
 		director_remove_host(conn->dir, conn->host, host);
 	return TRUE;
@@ -432,7 +432,7 @@
 		return FALSE;
 	}
 
-	host = mail_host_lookup(&ip);
+	host = mail_host_lookup(conn->dir->mail_hosts, &ip);
 	if (host == NULL) {
 		/* we probably just removed this host. */
 		return TRUE;
@@ -583,7 +583,7 @@
 	struct mail_host *const *hostp;
 
 	str_printfa(str, "HOST-HAND-START\t%u\n", conn->dir->ring_handshaked);
-	array_foreach(mail_hosts_get(), hostp) {
+	array_foreach(mail_hosts_get(conn->dir->mail_hosts), hostp) {
 		str_printfa(str, "HOST\t%s\t%u\n",
 			    net_ip2addr(&(*hostp)->ip), (*hostp)->vhost_count);
 	}
diff -r c57c1ff9c5f1 -r 19336bddada2 src/director/director-request.c
--- a/src/director/director-request.c	Thu May 20 11:46:21 2010 +0200
+++ b/src/director/director-request.c	Thu May 20 12:23:55 2010 +0200
@@ -90,7 +90,8 @@
 			/* delay adding new users until ring is again synced */
 			return FALSE;
 		}
-		host = mail_host_get_by_hash(request->username_hash);
+		host = mail_host_get_by_hash(dir->mail_hosts,
+					     request->username_hash);
 		if (host == NULL) {
 			/* all hosts have been removed */
 			return FALSE;
diff -r c57c1ff9c5f1 -r 19336bddada2 src/director/director.c
--- a/src/director/director.c	Thu May 20 11:46:21 2010 +0200
+++ b/src/director/director.c	Thu May 20 12:23:55 2010 +0200
@@ -147,7 +147,7 @@
 	director_update_send(dir, src, t_strdup_printf(
 		"HOST-REMOVE\t%s\n", net_ip2addr(&host->ip)));
 	user_directory_remove_host(dir->users, host);
-	mail_host_remove(host);
+	mail_host_remove(dir->mail_hosts, host);
 }
 
 void director_update_user(struct director *dir, struct director_host *src,
@@ -190,6 +190,7 @@
 	i_array_init(&dir->pending_requests, 16);
 	i_array_init(&dir->desynced_host_changes, 16);
 	dir->users = user_directory_init(set->director_user_expire);
+	dir->mail_hosts = mail_hosts_init();
 	return dir;
 }
 
@@ -206,6 +207,7 @@
 		director_connection_deinit(&dir->right);
 
 	user_directory_deinit(&dir->users);
+	mail_hosts_deinit(&dir->mail_hosts);
 	if (dir->to_request != NULL)
 		timeout_remove(&dir->to_request);
 	array_foreach(&dir->dir_hosts, hostp)
diff -r c57c1ff9c5f1 -r 19336bddada2 src/director/director.h
--- a/src/director/director.h	Thu May 20 11:46:21 2010 +0200
+++ b/src/director/director.h	Thu May 20 12:23:55 2010 +0200
@@ -30,6 +30,7 @@
 	struct director_host *self_host;
 	struct director_connection *left, *right;
 
+	struct mail_host_list *mail_hosts;
 	/* temporary user -> host associations */
 	struct user_directory *users;
 
diff -r c57c1ff9c5f1 -r 19336bddada2 src/director/doveadm-connection.c
--- a/src/director/doveadm-connection.c	Thu May 20 11:46:21 2010 +0200
+++ b/src/director/doveadm-connection.c	Thu May 20 12:23:55 2010 +0200
@@ -43,7 +43,7 @@
 	struct mail_host *const *hostp;
 	string_t *str = t_str_new(1024);
 
-	array_foreach(mail_hosts_get(), hostp) {
+	array_foreach(mail_hosts_get(conn->dir->mail_hosts), hostp) {
 		str_printfa(str, "%s\t%u\t%u\n",
 			    net_ip2addr(&(*hostp)->ip), (*hostp)->vhost_count,
 			    (*hostp)->user_count);
@@ -68,6 +68,7 @@
 static bool
 doveadm_cmd_host_set(struct doveadm_connection *conn, const char *line)
 {
+	struct director *dir = conn->dir;
 	const char *const *args;
 	struct mail_host *host;
 	struct ip_addr ip;
@@ -84,12 +85,12 @@
 		o_stream_send_str(conn->output, "vhost count too large\n");
 		return TRUE;
 	}
-	host = mail_host_lookup(&ip);
+	host = mail_host_lookup(dir->mail_hosts, &ip);
 	if (host == NULL)
-		host = mail_host_add_ip(&ip);
+		host = mail_host_add_ip(dir->mail_hosts, &ip);
 	if (vhost_count != -1U)
-		mail_host_set_vhost_count(host, vhost_count);
-	director_update_host(conn->dir, conn->dir->self_host, host);
+		mail_host_set_vhost_count(dir->mail_hosts, host, vhost_count);
+	director_update_host(dir, dir->self_host, host);
 
 	o_stream_send(conn->output, "OK\n", 3);
 	return TRUE;
@@ -105,7 +106,7 @@
 		i_error("doveadm sent invalid HOST-SET parameters");
 		return FALSE;
 	}
-	host = mail_host_lookup(&ip);
+	host = mail_host_lookup(conn->dir->mail_hosts, &ip);
 	if (host == NULL)
 		o_stream_send_str(conn->output, "NOTFOUND\n");
 	else {
diff -r c57c1ff9c5f1 -r 19336bddada2 src/director/mail-host.c
--- a/src/director/mail-host.c	Thu May 20 11:46:21 2010 +0200
+++ b/src/director/mail-host.c	Thu May 20 12:23:55 2010 +0200
@@ -6,9 +6,11 @@
 
 #define VHOST_MULTIPLIER 100
 
-static ARRAY_TYPE(mail_host) hosts;
-static ARRAY_DEFINE(vhosts, struct mail_host *);
-static bool hosts_unsorted;
+struct mail_host_list {
+	ARRAY_TYPE(mail_host) hosts;
+	ARRAY_DEFINE(vhosts, struct mail_host *);
+	bool hosts_unsorted;
+};
 
 static int
 mail_host_cmp(struct mail_host *const *h1, struct mail_host *const *h2)
@@ -16,36 +18,37 @@
 	return net_ip_cmp(&(*h1)->ip, &(*h2)->ip);
 }
 
-static void mail_hosts_sort(void)
+static void mail_hosts_sort(struct mail_host_list *list)
 {
 	struct mail_host *const *hostp;
 	unsigned int i;
 
-	array_sort(&hosts, mail_host_cmp);
+	array_sort(&list->hosts, mail_host_cmp);
 
 	/* rebuild vhosts */
-	array_clear(&vhosts);
-	array_foreach(&hosts, hostp) {
+	array_clear(&list->vhosts);
+	array_foreach(&list->hosts, hostp) {
 		for (i = 0; i < (*hostp)->vhost_count; i++)
-			array_append(&vhosts, hostp, 1);
+			array_append(&list->vhosts, hostp, 1);
 	}
-	hosts_unsorted = FALSE;
+	list->hosts_unsorted = FALSE;
 }
 
-struct mail_host *mail_host_add_ip(const struct ip_addr *ip)
+struct mail_host *
+mail_host_add_ip(struct mail_host_list *list, const struct ip_addr *ip)
 {
 	struct mail_host *host;
 
 	host = i_new(struct mail_host, 1);
 	host->vhost_count = VHOST_MULTIPLIER;
 	host->ip = *ip;
-	array_append(&hosts, &host, 1);
+	array_append(&list->hosts, &host, 1);
 
-	hosts_unsorted = TRUE;
+	list->hosts_unsorted = TRUE;
 	return host;
 }
 
-static int mail_host_add(const char *host)
+static int mail_host_add(struct mail_host_list *list, const char *host)
 {
 	struct ip_addr ip;
 
@@ -54,11 +57,12 @@
 		return -1;
 	}
 
-	mail_host_add_ip(&ip);
+	mail_host_add_ip(list, &ip);
 	return 0;
 }
 
-static int mail_hosts_add_range(const char *host1, const char *host2)
+static int
+mail_hosts_add_range(struct mail_host_list *list, const char *host1, const char *host2)
 {
 	struct ip_addr ip1, ip2;
 
@@ -76,26 +80,27 @@
 	return 0;
 }
 
-int mail_hosts_parse_and_add(const char *hosts_list)
+int mail_hosts_parse_and_add(struct mail_host_list *list,
+			     const char *hosts_string)
 {
 	int ret = 0;
 
 	T_BEGIN {
 		const char *const *tmp, *p;
 
-		tmp = t_strsplit_spaces(hosts_list, " ");
+		tmp = t_strsplit_spaces(hosts_string, " ");
 		for (; *tmp != NULL; tmp++) {
 			p = strchr(*tmp, '-');
 			if (p == NULL) {
-				if (mail_host_add(*tmp) < 0)
+				if (mail_host_add(list, *tmp) < 0)
 					ret = -1;
-			} else if (mail_hosts_add_range(t_strdup_until(*tmp, p),
+			} else if (mail_hosts_add_range(list, t_strdup_until(*tmp, p),
 							p + 1) < 0)
 				ret = -1;
 		}
 	} T_END;
 
-	if (array_count(&hosts) == 0) {
+	if (array_count(&list->hosts) == 0) {
 		if (ret < 0)
 			i_error("No valid servers specified");
 		else
@@ -105,78 +110,88 @@
 	return ret;
 }
 
-void mail_host_set_vhost_count(struct mail_host *host,
-			       unsigned int vhost_count)
+void mail_host_set_vhost_count(struct mail_host_list *list,
+			       struct mail_host *host, unsigned int vhost_count)
 {
 	host->vhost_count = vhost_count;
-	mail_hosts_sort();
+	mail_hosts_sort(list);
 }
 
-void mail_host_remove(struct mail_host *host)
+void mail_host_remove(struct mail_host_list *list, struct mail_host *host)
 {
-	struct mail_host *const *h;
+	struct mail_host *const *hosts;
 	unsigned int i, count;
 
-	h = array_get(&hosts, &count);
+	hosts = array_get(&list->hosts, &count);
 	for (i = 0; i < count; i++) {
-		if (h[i] == host) {
-			array_delete(&hosts, i, 1);
+		if (hosts[i] == host) {
+			array_delete(&list->hosts, i, 1);


More information about the dovecot-cvs mailing list