diff --git a/ChangeLog.txt b/ChangeLog.txt index e794a95a..09bb0eec 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -5,6 +5,10 @@ Broker: - Fix slow websockets performance when sending large messages. Closes #1390. - Fix bridges potentially not connecting on Windows. Closes #478. +Client library: +- Fix reconnect backoff for the situation where connections are dropped rather + than refused. Closes #737. + Documentation: - Improve details on global/per listener options in the mosquitto.conf man page. Closes #274. diff --git a/lib/handle_connack.c b/lib/handle_connack.c index 60780f6c..ab66ff0f 100644 --- a/lib/handle_connack.c +++ b/lib/handle_connack.c @@ -31,6 +31,9 @@ Contributors: static void connack_callback(struct mosquitto *mosq, uint8_t reason_code, uint8_t connect_flags, const mosquitto_property *properties) { log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received CONNACK (%d)", mosq->id, reason_code); + if(reason_code == MQTT_RC_SUCCESS){ + mosq->reconnects = 0; + } pthread_mutex_lock(&mosq->callback_mutex); if(mosq->on_connect){ mosq->in_callback = true; diff --git a/lib/loop.c b/lib/loop.c index 36763e06..0f751a9c 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -194,7 +194,6 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) { int run = 1; int rc; - unsigned int reconnects = 0; unsigned long reconnect_delay; #ifndef WIN32 struct timespec req, rem; @@ -202,6 +201,8 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) if(!mosq) return MOSQ_ERR_INVAL; + mosq->reconnects = 0; + if(mosq->state == mosq_cs_connect_async){ mosquitto_reconnect(mosq); } @@ -209,9 +210,6 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) while(run){ do{ rc = mosquitto_loop(mosq, timeout, max_packets); - if (reconnects !=0 && rc == MOSQ_ERR_SUCCESS){ - reconnects = 0; - } }while(run && rc == MOSQ_ERR_SUCCESS); /* Quit after fatal errors. */ switch(rc){ @@ -245,9 +243,9 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) if(mosq->reconnect_delay_max > mosq->reconnect_delay){ if(mosq->reconnect_exponential_backoff){ - reconnect_delay = mosq->reconnect_delay*(reconnects+1)*(reconnects+1); + reconnect_delay = mosq->reconnect_delay*(mosq->reconnects+1)*(mosq->reconnects+1); }else{ - reconnect_delay = mosq->reconnect_delay*(reconnects+1); + reconnect_delay = mosq->reconnect_delay*(mosq->reconnects+1); } }else{ reconnect_delay = mosq->reconnect_delay; @@ -256,7 +254,7 @@ int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets) if(reconnect_delay > mosq->reconnect_delay_max){ reconnect_delay = mosq->reconnect_delay_max; }else{ - reconnects++; + mosq->reconnects++; } #ifdef WIN32 diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index ff656a43..da16dae0 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -326,6 +326,7 @@ struct mosquitto { char *host; int port; char *bind_address; + unsigned int reconnects; unsigned int reconnect_delay; unsigned int reconnect_delay_max; bool reconnect_exponential_backoff;