diff src/lib/network.c @ 10158:1bc88aa1373f HEAD

Added net_connect_unix_with_retries().
author Timo Sirainen <tss@iki.fi>
date Thu, 22 Oct 2009 22:25:08 -0400
parents 95fba8612a87
children 615eef3139c2
line wrap: on
line diff
--- 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>
@@ -249,6 +250,27 @@
 	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;
+}
+
 void net_disconnect(int fd)
 {
 	if (close(fd) < 0)