Add retry (count, slope and limit) option

This commit is contained in:
angt
2015-12-02 16:05:15 +01:00
parent 723006a10d
commit bd46acb672

View File

@@ -649,11 +649,17 @@ int main (int argc, char **argv)
char *dev = PACKAGE_NAME; char *dev = PACKAGE_NAME;
char *keyfile = NULL; char *keyfile = NULL;
char *congestion = NULL; char *congestion = NULL;
long buffer_size = GT_BUFFER_SIZE; long buffer_size = GT_BUFFER_SIZE;
long ka_count = -1; long ka_count = -1;
long ka_idle = -1; long ka_idle = -1;
long ka_interval = -1; long ka_interval = -1;
long retry_count = 0;
long retry_slope = 1000;
long retry_limit = 1000000;
struct option ka_opts[] = { struct option ka_opts[] = {
{ "count", &ka_count, option_long }, { "count", &ka_count, option_long },
{ "idle", &ka_idle, option_long }, { "idle", &ka_idle, option_long },
@@ -665,6 +671,12 @@ int main (int argc, char **argv)
{ "fake", NULL, option_option }, { "fake", NULL, option_option },
}; };
struct option retry_opts[] = {
{ "count", &retry_count, option_long },
{ "slope", &retry_slope, option_long },
{ "limit", &retry_limit, option_long },
};
struct option opts[] = { struct option opts[] = {
{ "listener", NULL, option_option }, { "listener", NULL, option_option },
{ "host", &host, option_str }, { "host", &host, option_str },
@@ -677,6 +689,7 @@ int main (int argc, char **argv)
{ "keepalive", ka_opts, option_option }, { "keepalive", ka_opts, option_option },
{ "buffer-size", &buffer_size, option_long }, { "buffer-size", &buffer_size, option_long },
{ "noquickack", NULL, option_option }, { "noquickack", NULL, option_option },
{ "retry", &retry_opts, option_option },
{ "daemon", &daemon_opts, option_option }, { "daemon", &daemon_opts, option_option },
{ "version", NULL, option_option }, { "version", NULL, option_option },
{ NULL }, { NULL },
@@ -770,14 +783,38 @@ int main (int argc, char **argv)
} }
} }
long retry = 0;
while (!gt_close) { while (!gt_close) {
sock.fd = listener?sk_accept(fd):sk_create(ai, sk_connect); sock.fd = listener?sk_accept(fd):sk_create(ai, sk_connect);
if (sock.fd==-1) { if (sock.fd==-1) {
usleep(100000); if (retry<LONG_MAX)
retry++;
long usec = retry*retry_slope;
if (retry_count>=0 && retry>=retry_count) {
gt_log("couldn't %s (%d attempt%s)\n",
listener?"listen":"connect",
retry, (retry>1)?"s":"");
break;
}
if (usec>retry_limit)
usec = retry_limit;
if (usec<=0)
usec = 0;
if (usleep(usec)==-1 && errno==EINVAL)
sleep(usec/1000000);
continue; continue;
} }
retry = 0;
char *sockname = sk_get_name(sock.fd); char *sockname = sk_get_name(sock.fd);
if (!sockname) { if (!sockname) {