From 529b8fab8c21e7ae5af91d742ff202eab38e51f3 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 13 Jan 2020 14:23:15 +0100 Subject: [PATCH] Add a configurable fast connection retry period. If no nodes are reachable, allow connections to retry once every second for a per device-class configurable amount of time. --- src/graph.c | 20 ++++++++++++++++++++ src/meshlink++.h | 15 ++++++++++++++- src/meshlink.c | 16 ++++++++++++++++ src/meshlink.h | 11 +++++++++++ src/meshlink.sym | 1 + src/meshlink_internal.h | 3 +++ src/net.c | 8 ++++++-- src/net_setup.c | 1 + src/net_socket.c | 8 ++++++-- 9 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/graph.c b/src/graph.c index 5afa7c32..18376c14 100644 --- a/src/graph.c +++ b/src/graph.c @@ -136,7 +136,13 @@ static void sssp_bfs(meshlink_handle_t *mesh) { static void check_reachability(meshlink_handle_t *mesh) { /* Check reachability status. */ + int reachable = -1; /* Don't count ourself */ + for splay_each(node_t, n, mesh->nodes) { + if(n->status.visited) { + reachable++; + } + /* Check for nodes that have changed session_id */ if(n->status.visited && n->prevedge && n->prevedge->reverse->session_id != n->session_id) { n->session_id = n->prevedge->reverse->session_id; @@ -207,6 +213,20 @@ static void check_reachability(meshlink_handle_t *mesh) { } } } + + if(mesh->reachable != reachable) { + if(!reachable) { + mesh->last_unreachable = mesh->loop.now.tv_sec; + + if(mesh->threadstarted) { + timeout_set(&mesh->loop, &mesh->periodictimer, &(struct timeval) { + 0, prng(mesh, TIMER_FUDGE) + }); + } + } + + mesh->reachable = reachable; + } } void graph(meshlink_handle_t *mesh) { diff --git a/src/meshlink++.h b/src/meshlink++.h index 74ec06e5..3599cc55 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -990,7 +990,20 @@ public: * @param pingtimeout The required time within which a peer should respond, in seconds. The default is 5. * The timeout must be smaller than the interval. */ - void set_dev_class_timeouts(dev_class_t devclass, int pinginterval, int pingtimeout); + void set_dev_class_timeouts(dev_class_t devclass, int pinginterval, int pingtimeout) { + meshlink_set_dev_class_timeouts(handle, devclass, pinginterval, pingtimeout); + } + + /// Set device class fast retry period + /** This sets the fast retry period for a given device class. + * During this period after the last time the mesh becomes unreachable, connections are tried once a second. + * + * @param devclass The device class to update + * @param fast_retry_period The period during which fast connection retries are done. The default is 0. + */ + void set_dev_class_fast_retry_period(dev_class_t devclass, int fast_retry_period) { + meshlink_set_dev_class_fast_retry_period(handle, devclass, fast_retry_period); + } private: // non-copyable: diff --git a/src/meshlink.c b/src/meshlink.c index c4613a70..30b38684 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -3807,6 +3807,22 @@ void meshlink_set_dev_class_timeouts(meshlink_handle_t *mesh, dev_class_t devcla pthread_mutex_unlock(&mesh->mutex); } +void meshlink_set_dev_class_fast_retry_period(meshlink_handle_t *mesh, dev_class_t devclass, int fast_retry_period) { + if(!mesh || devclass < 0 || devclass >= DEV_CLASS_COUNT) { + meshlink_errno = EINVAL; + return; + } + + if(fast_retry_period < 0) { + meshlink_errno = EINVAL; + return; + } + + pthread_mutex_lock(&mesh->mutex); + mesh->dev_class_traits[devclass].fast_retry_period = fast_retry_period; + pthread_mutex_unlock(&mesh->mutex); +} + void handle_network_change(meshlink_handle_t *mesh, bool online) { (void)online; diff --git a/src/meshlink.h b/src/meshlink.h index 98a34bd6..e3a7ca94 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -1524,6 +1524,17 @@ extern bool meshlink_encrypted_key_rotate(struct meshlink_handle *mesh, const vo */ extern void meshlink_set_dev_class_timeouts(struct meshlink_handle *mesh, dev_class_t devclass, int pinginterval, int pingtimeout); +/// Set device class fast retry period +/** This sets the fast retry period for a given device class. + * During this period after the last time the mesh becomes unreachable, connections are tried once a second. + * + * \memberof meshlink_handle + * @param mesh A handle which represents an instance of MeshLink. + * @param devclass The device class to update + * @param fast_retry_period The period during which fast connection retries are done. The default is 0. + */ +extern void meshlink_set_dev_class_fast_retry_period(struct meshlink_handle *mesh, dev_class_t devclass, int fast_retry_period); + #ifdef __cplusplus } #endif diff --git a/src/meshlink.sym b/src/meshlink.sym index 674a260e..b0ad2e52 100644 --- a/src/meshlink.sym +++ b/src/meshlink.sym @@ -65,6 +65,7 @@ meshlink_set_channel_receive_cb meshlink_set_channel_sndbuf meshlink_set_connection_try_cb meshlink_set_default_blacklist +meshlink_set_dev_class_fast_retry_period meshlink_set_dev_class_timeouts meshlink_set_error_cb meshlink_set_invitation_timeout diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 15edba9a..6aa87171 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -82,6 +82,7 @@ struct meshlink_open_params { typedef struct { int pinginterval; int pingtimeout; + int fast_retry_period; unsigned int min_connects; unsigned int max_connects; int edge_weight; @@ -101,6 +102,7 @@ struct meshlink_handle { meshlink_log_level_t log_level; // The most important network-related members come first + int reachable; int listen_sockets; listen_socket_t listen_socket[MAXSOCKETS]; @@ -128,6 +130,7 @@ struct meshlink_handle { time_t connection_burst_time; time_t last_config_check; time_t last_hard_try; + time_t last_unreachable; timeout_t pingtimer; timeout_t periodictimer; diff --git a/src/net.c b/src/net.c index b9f441a0..7c600534 100644 --- a/src/net.c +++ b/src/net.c @@ -118,6 +118,10 @@ static void timeout_handler(event_loop_t *loop, void *data) { int pingtimeout = c->node ? mesh->dev_class_traits[c->node->devclass].pingtimeout : default_timeout; int pinginterval = c->node ? mesh->dev_class_traits[c->node->devclass].pinginterval : default_interval; + if(c->outgoing && c->outgoing->timeout < 5) { + pingtimeout = 1; + } + // Also make sure that if outstanding key requests for the UDP counterpart of a connection has timed out, we restart it. if(c->node) { if(c->node->status.waitingforkey && c->node->last_req_key + pingtimeout <= mesh->loop.now.tv_sec) { @@ -148,7 +152,7 @@ static void timeout_handler(event_loop_t *loop, void *data) { } timeout_set(&mesh->loop, data, &(struct timeval) { - default_timeout, prng(mesh, TIMER_FUDGE) + 1, prng(mesh, TIMER_FUDGE) }); } @@ -701,7 +705,7 @@ void retry(meshlink_handle_t *mesh) { */ void main_loop(meshlink_handle_t *mesh) { timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval) { - default_timeout, prng(mesh, TIMER_FUDGE) + 1, prng(mesh, TIMER_FUDGE) }); timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval) { 0, 0 diff --git a/src/net_setup.c b/src/net_setup.c index b5c25d03..8baff8b8 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -452,6 +452,7 @@ bool setup_myself(meshlink_handle_t *mesh) { /* Done. */ mesh->last_config_check = mesh->loop.now.tv_sec; + mesh->last_unreachable = mesh->loop.now.tv_sec; return true; } diff --git a/src/net_socket.c b/src/net_socket.c index 31bc608f..eb9e3fc4 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -246,14 +246,18 @@ static void retry_outgoing_handler(event_loop_t *loop, void *data) { } void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) { - outgoing->timeout += 5; + if(!mesh->reachable && mesh->loop.now.tv_sec < mesh->last_unreachable + mesh->dev_class_traits[mesh->devclass].fast_retry_period) { + outgoing->timeout = 1; + } else { + outgoing->timeout += 5; + } if(outgoing->timeout > mesh->maxtimeout) { outgoing->timeout = mesh->maxtimeout; } timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval) { - outgoing->timeout, rand() % 100000 + outgoing->timeout, prng(mesh, TIMER_FUDGE) }); logger(mesh, MESHLINK_INFO, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout); -- 2.39.5