From 5d02f5815181aae427949641e3a612c3e4daa01b Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 4 Dec 2018 17:19:31 +0000 Subject: [PATCH] Fix reconnect delay backoff behaviour. Closes #1027. Thanks to Harm Verhagen. Bug: https://github.com/eclipse/mosquitto/issues/1027 --- ChangeLog.txt | 3 +++ lib/loop.c | 8 ++++++-- lib/options.c | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 953a2b3c..969c6c2a 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -10,6 +10,9 @@ Broker: because this can lead to confusing "Protocol not supported" errors if the network is down. Closes #1062. +Library: +- Fix reconnect delay backoff behaviour. Closes #1027. + Client: - Always print leading zeros in mosquitto_sub when output format is hex. Closes #1066. diff --git a/lib/loop.c b/lib/loop.c index 23e60825..349ee5dc 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -245,8 +245,12 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) }else{ pthread_mutex_unlock(&mosq->state_mutex); - if(mosq->reconnect_delay > 0 && mosq->reconnect_exponential_backoff){ - reconnect_delay = mosq->reconnect_delay*reconnects*reconnects; + if(mosq->reconnect_delay_max > mosq->reconnect_delay){ + if(mosq->reconnect_exponential_backoff){ + reconnect_delay = mosq->reconnect_delay*(reconnects+1)*(reconnects+1); + }else{ + reconnect_delay = mosq->reconnect_delay*(reconnects+1); + } }else{ reconnect_delay = mosq->reconnect_delay; } diff --git a/lib/options.c b/lib/options.c index dd9f7180..00951a68 100644 --- a/lib/options.c +++ b/lib/options.c @@ -76,6 +76,8 @@ int mosquitto_reconnect_delay_set(struct mosquitto *mosq, unsigned int reconnect { if(!mosq) return MOSQ_ERR_INVAL; + if(reconnect_delay == 0) reconnect_delay = 1; + mosq->reconnect_delay = reconnect_delay; mosq->reconnect_delay_max = reconnect_delay_max; mosq->reconnect_exponential_backoff = reconnect_exponential_backoff;