From: Guus Sliepen Date: Thu, 7 Aug 2014 15:47:58 +0000 (+0200) Subject: Speed up making outgoing connections. X-Git-Url: http://git.meshlink.io/?p=meshlink;a=commitdiff_plain;h=5809d16e2bc41dbc90358f4324b77482f4ca03de Speed up making outgoing connections. - Immediately fire the periodic_handler() when the main loop starts. - Use a very short timeout if we haven't made enough real and pending connections yet to fill the autoconnect budget. This basically means we immediately start up to 3 outgoing connections to different nodes. --- diff --git a/src/net.c b/src/net.c index d8f7d642..20b799ff 100644 --- a/src/net.c +++ b/src/net.c @@ -31,6 +31,10 @@ #include "protocol.h" #include "xalloc.h" +static const int min(int a, int b) { + return a < b ? a : b; +} + /* Terminate a connection: - Mark it as inactive @@ -210,6 +214,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { mesh->contradicting_add_edge = 0; mesh->contradicting_del_edge = 0; + int timeout = 5; + /* If AutoConnect is set, check if we need to make or break connections. */ if(autoconnect && mesh->nodes->count > 1) { @@ -298,9 +304,12 @@ static void periodic_handler(event_loop_t *loop, void *data) { } } } + + if (nc + mesh->outgoings->count < min(autoconnect, mesh->nodes->count - 1)) + timeout = 0; } - timeout_set(&mesh->loop, data, &(struct timeval){5, rand() % 100000}); + timeout_set(&mesh->loop, data, &(struct timeval){timeout, rand() % 100000}); } void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) { @@ -333,7 +342,7 @@ void retry(meshlink_handle_t *mesh) { */ int main_loop(meshlink_handle_t *mesh) { timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval){mesh->pingtimeout, rand() % 100000}); - timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){mesh->pingtimeout, rand() % 100000}); + timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){0, 0}); //Add signal handler mesh->datafromapp.signum = 0;