dovecot-2.0-sslstream: Added net_connect_unix_with_retries().

dovecot at dovecot.org dovecot at dovecot.org
Sat Feb 13 02:55:32 EET 2010


details:   http://hg.dovecot.org/dovecot-2.0-sslstream/rev/1bc88aa1373f
changeset: 10159:1bc88aa1373f
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Oct 22 22:25:08 2009 -0400
description:
Added net_connect_unix_with_retries().

diffstat:

2 files changed, 25 insertions(+)
src/lib/network.c |   22 ++++++++++++++++++++++
src/lib/network.h |    3 +++

diffs (52 lines):

diff -r faf2339212b8 -r 1bc88aa1373f src/lib/network.c
--- a/src/lib/network.c	Thu Oct 22 22:12:56 2009 -0400
+++ b/src/lib/network.c	Thu Oct 22 22:25:08 2009 -0400
@@ -3,6 +3,7 @@
 #include "lib.h"
 #include "close-keep-errno.h"
 #include "fd-set-nonblock.h"
+#include "time-util.h"
 #include "network.h"
 
 #include <stdlib.h>
@@ -246,6 +247,27 @@ int net_connect_unix(const char *path)
 		return -1;
 	}
 
+	return fd;
+}
+
+int net_connect_unix_with_retries(const char *path, unsigned int msecs)
+{
+	struct timeval start, now;
+	int fd;
+
+	if (gettimeofday(&start, NULL) < 0)
+		i_panic("gettimeofday() failed: %m");
+
+	do {
+		fd = net_connect_unix(path);
+		if (fd != -1 || (errno != EAGAIN && errno != ECONNREFUSED))
+			break;
+
+		/* busy. wait for a while. */
+		usleep(((rand() % 10) + 1) * 10000);
+		if (gettimeofday(&now, NULL) < 0)
+			i_panic("gettimeofday() failed: %m");
+	} while (timeval_diff_msecs(&now, &start) < (int)msecs);
 	return fd;
 }
 
diff -r faf2339212b8 -r 1bc88aa1373f src/lib/network.h
--- a/src/lib/network.h	Thu Oct 22 22:12:56 2009 -0400
+++ b/src/lib/network.h	Thu Oct 22 22:25:08 2009 -0400
@@ -49,6 +49,9 @@ int net_connect_ip(const struct ip_addr 
 		   const struct ip_addr *my_ip);
 /* Connect to named UNIX socket */
 int net_connect_unix(const char *path);
+/* Try to connect to UNIX socket for give number of seconds when connect()
+   returns EAGAIN or ECONNREFUSED. */
+int net_connect_unix_with_retries(const char *path, unsigned int msecs);
 /* Disconnect socket */
 void net_disconnect(int fd);
 


More information about the dovecot-cvs mailing list