From: Timo Sirainen Date: Fri, 23 Oct 2009 02:25:08 +0000 (-0400) Subject: Added net_connect_unix_with_retries(). X-Git-Tag: 2.0.alpha2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fb00ee776fa8a88bcdf887500e2e22e723546e5d;p=thirdparty%2Fdovecot%2Fcore.git Added net_connect_unix_with_retries(). --HG-- branch : HEAD --- diff --git a/src/lib/network.c b/src/lib/network.c index cb31049de0..15d3ec0581 100644 --- a/src/lib/network.c +++ b/src/lib/network.c @@ -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 @@ -249,6 +250,27 @@ int net_connect_unix(const char *path) 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) diff --git a/src/lib/network.h b/src/lib/network.h index 916f49c569..4cb6abdecd 100644 --- a/src/lib/network.h +++ b/src/lib/network.h @@ -49,6 +49,9 @@ int net_connect_ip(const struct ip_addr *ip, unsigned int port, 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);