Skip to content

Commit

Permalink
Fix bridge backoff calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Mar 11, 2021
1 parent 9a87603 commit 88d2c74
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Broker:
- Fix messages to `$` prefixed topics being rejected. Closes #2111.
- Fix QoS 0 messages not being delivered when max_queued_bytes was configured.
Closes #2123.
- Fix bridge increasing backoff calculation.

Client library:
- Fix encrypted connections incorrectly connecting when the CA file passed to
Expand Down
32 changes: 30 additions & 2 deletions src/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,11 @@ void bridge__packet_cleanup(struct mosquitto *context)
packet__cleanup(&(context->in_packet));
}

static int rand_between(int base, int cap)
static int rand_between(int low, int high)
{
int r;
util__random_bytes(&r, sizeof(int));
return (r % (cap - base)) + base;
return (abs(r) % (high - low)) + low;
}

static void bridge__backoff_step(struct mosquitto *context)
Expand Down Expand Up @@ -684,6 +684,33 @@ static void bridge__backoff_reset(struct mosquitto *context)
}
}


static void bridge_check_pending(struct mosquitto *context)
{
int err;
socklen_t len;

if(context->state == mosq_cs_connect_pending){
len = sizeof(int);
if(!getsockopt(context->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &len)){
if(err == 0){
mosquitto__set_state(context, mosq_cs_new);
#if defined(WITH_ADNS) && defined(WITH_BRIDGE)
if(context->bridge){
bridge__connect_step3(context);
}
#endif
}else if(err == ECONNREFUSED){
do_disconnect(context, MOSQ_ERR_CONN_LOST);
return;
}
}else{
do_disconnect(context, MOSQ_ERR_CONN_LOST);
return;
}
}
}

void bridge_check(void)
{
static time_t last_check = 0;
Expand All @@ -702,6 +729,7 @@ void bridge_check(void)

if(context->sock != INVALID_SOCKET){
mosquitto__check_keepalive(context);
bridge_check_pending(context);

/* Check for bridges that are not round robin and not currently
* connected to their primary broker. */
Expand Down

0 comments on commit 88d2c74

Please sign in to comment.