dovecot-2.0: dict proxy: Disconnect from dict server after 1 sec...

dovecot at dovecot.org dovecot at dovecot.org
Fri Apr 16 16:14:18 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/bf08bb1807cf
changeset: 11162:bf08bb1807cf
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Apr 16 16:14:15 2010 +0300
description:
dict proxy: Disconnect from dict server after 1 second of idling.
Reconnecting is pretty cheap and it's not good to have many idling dict
server processes.

diffstat:

 src/lib-dict/dict-client.c |  39 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 39 insertions(+), 0 deletions(-)

diffs (97 lines):

diff -r 69894aaab23c -r bf08bb1807cf src/lib-dict/dict-client.c
--- a/src/lib-dict/dict-client.c	Fri Apr 16 16:13:36 2010 +0300
+++ b/src/lib-dict/dict-client.c	Fri Apr 16 16:14:15 2010 +0300
@@ -13,6 +13,12 @@
 #include <unistd.h>
 #include <fcntl.h>
 
+/* Disconnect from dict server after this many milliseconds of idling after
+   sending a command. This timeout is short, because dict server does blocking
+   dict accesses, so it can handle only one client at a time. increasing the
+   timeout increases number of idling dict processes. */
+#define DICT_CLIENT_TIMEOUT_MSECS 1000
+
 struct client_dict {
 	struct dict dict;
 
@@ -27,6 +33,7 @@
 	struct istream *input;
 	struct ostream *output;
 	struct io *io;
+	struct timeout *to_idle;
 
 	struct client_dict_transaction_context *transactions;
 
@@ -311,12 +318,36 @@
 	return 1;
 }
 
+static bool client_dict_is_finished(struct client_dict *dict)
+{
+	return dict->transactions == NULL && !dict->in_iteration &&
+		dict->async_commits == 0;
+}
+
+static void client_dict_timeout(struct client_dict *dict)
+{
+	if (client_dict_is_finished(dict))
+		client_dict_disconnect(dict);
+}
+
+static void client_dict_add_timeout(struct client_dict *dict)
+{
+	if (dict->to_idle != NULL)
+		timeout_reset(dict->to_idle);
+	else if (client_dict_is_finished(dict)) {
+		dict->to_idle = timeout_add(DICT_CLIENT_TIMEOUT_MSECS,
+					    client_dict_timeout, dict);
+	}
+}
+
 static char *client_dict_read_line(struct client_dict *dict)
 {
 	char *line;
 
 	while (client_dict_read_one_line(dict, &line) == 0)
 		;
+
+	client_dict_add_timeout(dict);
 	return line;
 }
 
@@ -366,6 +397,8 @@
 	dict->connect_counter++;
 	dict->handshaked = FALSE;
 
+	if (dict->to_idle != NULL)
+		timeout_remove(&dict->to_idle);
 	if (dict->io != NULL)
 		io_remove(&dict->io);
 	if (dict->input != NULL)
@@ -561,6 +594,8 @@
 	pool_unref(&ctx->pool);
 	i_free(ctx);
 	dict->in_iteration = FALSE;
+
+	client_dict_add_timeout(dict);
 	return ret;
 }
 
@@ -639,6 +674,8 @@
 	if (ret < 0 || !async) {
 		DLLIST_REMOVE(&dict->transactions, ctx);
 		i_free(ctx);
+
+		client_dict_add_timeout(dict);
 	}
 	return ret;
 }
@@ -660,6 +697,8 @@
 
 	DLLIST_REMOVE(&dict->transactions, ctx);
 	i_free(ctx);
+
+	client_dict_add_timeout(dict);
 }
 
 static void client_dict_set(struct dict_transaction_context *_ctx,


More information about the dovecot-cvs mailing list