changeset 21076:ef9d8f4c6c67

director: Code cleanup - moved username_hash generation to director.h It's not really user_directory specific. This is especially important in the following patches that add per-tag user_directories. It's also not always known which tag the username_hash refers to, so there can't be different tag-specific username_hash generators.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 20 Oct 2016 20:31:33 +0300
parents 6a363cb51d7f
children b2418f9c5a54
files src/director/director-request.c src/director/director.c src/director/director.h src/director/doveadm-connection.c src/director/notify-connection.c src/director/test-user-directory.c src/director/user-directory.c src/director/user-directory.h
diffstat 8 files changed, 19 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/director/director-request.c	Thu Oct 20 19:38:28 2016 +0300
+++ b/src/director/director-request.c	Thu Oct 20 20:31:33 2016 +0300
@@ -126,7 +126,7 @@
 {
 	struct director_request *request;
 	unsigned int username_hash =
-		user_directory_get_username_hash(dir->users, username);
+		director_get_username_hash(dir, username);
 
 	dir->num_requests++;
 
--- a/src/director/director.c	Thu Oct 20 19:38:28 2016 +0300
+++ b/src/director/director.c	Thu Oct 20 20:31:33 2016 +0300
@@ -12,6 +12,7 @@
 #include "istream.h"
 #include "ostream.h"
 #include "iostream-temp.h"
+#include "mail-user-hash.h"
 #include "user-directory.h"
 #include "mail-host.h"
 #include "director-host.h"
@@ -1296,7 +1297,6 @@
 	i_array_init(&dir->pending_requests, 16);
 	i_array_init(&dir->connections, 8);
 	dir->users = user_directory_init(set->director_user_expire,
-					 set->director_username_hash,
 					 director_user_freed);
 	dir->mail_hosts = mail_hosts_init(set->director_consistent_hashing);
 
@@ -1387,6 +1387,12 @@
 	i_free(iter);
 }
 
+unsigned int
+director_get_username_hash(struct director *dir, const char *username)
+{
+	return mail_user_hash(username, dir->set->director_username_hash);
+}
+
 void directors_init(void)
 {
 	user_move_throttle =
--- a/src/director/director.h	Thu Oct 20 19:38:28 2016 +0300
+++ b/src/director/director.h	Thu Oct 20 20:31:33 2016 +0300
@@ -235,6 +235,9 @@
 
 int director_connect_host(struct director *dir, struct director_host *host);
 
+unsigned int
+director_get_username_hash(struct director *dir, const char *username);
+
 void directors_init(void);
 void directors_deinit(void);
 
--- a/src/director/doveadm-connection.c	Thu Oct 20 19:38:28 2016 +0300
+++ b/src/director/doveadm-connection.c	Thu Oct 20 20:31:33 2016 +0300
@@ -566,7 +566,7 @@
 		tag = args[1] != NULL ? args[1] : "";
 	}
 	if (str_to_uint(username, &username_hash) < 0)
-		username_hash = user_directory_get_username_hash(users, username);
+		username_hash = director_get_username_hash(conn->dir, username);
 
 	/* get user's current host */
 	user = user_directory_lookup(users, username_hash);
@@ -651,7 +651,7 @@
 	}
 
 	if (str_to_uint(args[0], &username_hash) < 0)
-		username_hash = user_directory_get_username_hash(users, args[0]);
+		username_hash = director_get_username_hash(conn->dir, args[0]);
 	user = user_directory_lookup(users, username_hash);
 	if (user != NULL && USER_IS_BEING_KILLED(user)) {
 		o_stream_nsend_str(conn->output, "TRYAGAIN\n");
--- a/src/director/notify-connection.c	Thu Oct 20 19:38:28 2016 +0300
+++ b/src/director/notify-connection.c	Thu Oct 20 20:31:33 2016 +0300
@@ -42,7 +42,7 @@
 	unsigned int hash;
 
 	while ((line = i_stream_read_next_line(conn->input)) != NULL) {
-		hash = user_directory_get_username_hash(conn->dir->users, line);
+		hash = director_get_username_hash(conn->dir->users, line);
 		notify_update_user(conn->dir, line, hash);
 	}
 	if (conn->input->eof) {
--- a/src/director/test-user-directory.c	Thu Oct 20 19:38:28 2016 +0300
+++ b/src/director/test-user-directory.c	Thu Oct 20 20:31:33 2016 +0300
@@ -42,7 +42,7 @@
 	unsigned int i;
 
 	test_begin("user directory ascending");
-	dir = user_directory_init(USER_DIR_TIMEOUT, "%u", NULL);
+	dir = user_directory_init(USER_DIR_TIMEOUT, NULL);
 	(void)user_directory_add(dir, 1, host, ioloop_time + count+1);
 
 	for (i = 0; i < count; i++)
@@ -60,7 +60,7 @@
 	unsigned int i;
 
 	test_begin("user directory descending");
-	dir = user_directory_init(USER_DIR_TIMEOUT, "%u", NULL);
+	dir = user_directory_init(USER_DIR_TIMEOUT, NULL);
 
 	for (i = 0; i < count; i++)
 		(void)user_directory_add(dir, i+1, host, ioloop_time - i);
@@ -77,7 +77,7 @@
 	unsigned int i, count = 10000 + rand()%10000;
 
 	test_begin("user directory random");
-	dir = user_directory_init(USER_DIR_TIMEOUT, "%u", NULL);
+	dir = user_directory_init(USER_DIR_TIMEOUT, NULL);
 	for (i = 0; i < count; i++) {
 		if (rand() % 10 == 0)
 			timestamp = ioloop_time;
--- a/src/director/user-directory.c	Thu Oct 20 19:38:28 2016 +0300
+++ b/src/director/user-directory.c	Thu Oct 20 20:31:33 2016 +0300
@@ -5,7 +5,6 @@
 #include "array.h"
 #include "hash.h"
 #include "llist.h"
-#include "mail-user-hash.h"
 #include "mail-host.h"
 #include "user-directory.h"
 
@@ -30,7 +29,6 @@
 	ARRAY(struct user_directory_iter *) iters;
 	void (*user_free_hook)(struct user *);
 
-	char *username_hash_fmt;
 	unsigned int timeout_secs;
 	/* If user's expire time is less than this many seconds away,
 	   don't assume that other directors haven't yet expired it */
@@ -256,12 +254,6 @@
 	array_free(&users);
 }
 
-unsigned int user_directory_get_username_hash(struct user_directory *dir,
-					      const char *username)
-{
-	return mail_user_hash(username, dir->username_hash_fmt);
-}
-
 bool user_directory_user_is_recently_updated(struct user_directory *dir,
 					     struct user *user)
 {
@@ -279,7 +271,7 @@
 }
 
 struct user_directory *
-user_directory_init(unsigned int timeout_secs, const char *username_hash_fmt,
+user_directory_init(unsigned int timeout_secs,
 		    void (*user_free_hook)(struct user *))
 {
 	struct user_directory *dir;
@@ -296,7 +288,6 @@
 		I_MAX(dir->user_near_expiring_secs, USER_NEAR_EXPIRING_MIN);
 	i_assert(dir->timeout_secs/2 > dir->user_near_expiring_secs);
 
-	dir->username_hash_fmt = i_strdup(username_hash_fmt);
 	dir->user_free_hook = user_free_hook;
 	hash_table_create_direct(&dir->hash, default_pool, 0);
 	i_array_init(&dir->iters, 8);
@@ -315,7 +306,6 @@
 		user_free(dir, dir->head);
 	hash_table_destroy(&dir->hash);
 	array_free(&dir->iters);
-	i_free(dir->username_hash_fmt);
 	i_free(dir);
 }
 
--- a/src/director/user-directory.h	Thu Oct 20 19:38:28 2016 +0300
+++ b/src/director/user-directory.h	Thu Oct 20 20:31:33 2016 +0300
@@ -29,7 +29,7 @@
 /* Create a new directory. Users are dropped if their time gets older
    than timeout_secs. */
 struct user_directory *
-user_directory_init(unsigned int timeout_secs, const char *username_hash_fmt,
+user_directory_init(unsigned int timeout_secs,
 		    void (*user_free_hook)(struct user *));
 void user_directory_deinit(struct user_directory **dir);
 
@@ -52,9 +52,6 @@
    timestamps based on remote director's user list after handshake. */
 void user_directory_sort(struct user_directory *dir);
 
-unsigned int user_directory_get_username_hash(struct user_directory *dir,
-					      const char *username);
-
 bool user_directory_user_is_recently_updated(struct user_directory *dir,
 					     struct user *user);
 bool user_directory_user_is_near_expiring(struct user_directory *dir,