Skip to content

Commit

Permalink
Fix reconnect backoff where connections are dropped
Browse files Browse the repository at this point in the history
Closes #737. Thanks to chelliwell.
  • Loading branch information
ralight committed Sep 17, 2019
1 parent 982758a commit cf7ac45
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.txt
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/handle_connack.c
Expand Up @@ -31,6 +31,9 @@ and the Eclipse Distribution License is available at
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;
Expand Down
12 changes: 5 additions & 7 deletions lib/loop.c
Expand Up @@ -194,24 +194,22 @@ 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;
#endif

if(!mosq) return MOSQ_ERR_INVAL;

mosq->reconnects = 0;

if(mosq->state == mosq_cs_connect_async){
mosquitto_reconnect(mosq);
}

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){
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/mosquitto_internal.h
Expand Up @@ -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;
Expand Down

0 comments on commit cf7ac45

Please sign in to comment.