Skip to content

Commit

Permalink
fixes nanomsg#1230 NNG_OPT_RECONNMAXT zero does not prevent exponenti…
Browse files Browse the repository at this point in the history
…al back-off
  • Loading branch information
gdamore committed May 24, 2020
1 parent 6f084d8 commit a052c50
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1375,26 +1375,28 @@ nni_ctx_setopt(
static void
dialer_timer_start_locked(nni_dialer *d)
{
nni_duration backoff;
nni_duration back_off;
nni_sock * sock = d->d_sock;

if (d->d_closing || sock->s_closed) {
return;
}
backoff = d->d_currtime;
d->d_currtime *= 2;
if ((d->d_maxrtime > 0) && (d->d_currtime > d->d_maxrtime)) {
d->d_currtime = d->d_maxrtime;
back_off = d->d_currtime;
if (d->d_maxrtime > 0) {
d->d_currtime *= 2;
if (d->d_currtime > d->d_maxrtime) {
d->d_currtime = d->d_maxrtime;
}
}

// To minimize damage from storms, etc., we select a backoff
// value randomly, in the range of [0, backoff-1]; this is
// pretty similar to 802 style backoff, except that we have a
// To minimize damage from storms, etc., we select a back-off
// value randomly, in the range of [0, back_off-1]; this is
// pretty similar to 802 style back-off, except that we have a
// nearly uniform time period instead of discrete slot times.
// This algorithm may lead to slight biases because we don't
// have a statistically perfect distribution with the modulo of
// the random number, but this really doesn't matter.
nni_sleep_aio(backoff ? nni_random() % backoff : 0, d->d_tmo_aio);
nni_sleep_aio(back_off ? nni_random() % back_off : 0, d->d_tmo_aio);
}

void
Expand Down
40 changes: 39 additions & 1 deletion tests/reconnect.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <nng/nng.h>
#include <nng/protocol/pipeline0/pull.h>
#include <nng/protocol/pipeline0/push.h>
#include <nng/supplemental/util/platform.h>

#include "acutest.h"
#include "testutil.h"
Expand Down Expand Up @@ -83,7 +84,7 @@ test_reconnect_pipe(void)
nng_socket push;
nng_socket pull;
nng_listener l;
nng_msg *msg;
nng_msg * msg;
char addr[64];

testutil_scratch_addr("inproc", sizeof(addr), addr);
Expand Down Expand Up @@ -121,9 +122,46 @@ test_reconnect_pipe(void)
TEST_NNG_PASS(nng_close(pull));
}

void
test_reconnect_back_off_zero(void)
{
nng_socket push;
nng_socket pull;
nng_time start;
char addr[64];
testutil_scratch_addr("inproc", sizeof(addr), addr);

TEST_NNG_PASS(nng_push0_open(&push));
TEST_NNG_PASS(nng_pull0_open(&pull));

// redial every 10 ms.
TEST_NNG_PASS(nng_setopt_ms(push, NNG_OPT_RECONNMAXT, 0));
TEST_NNG_PASS(nng_setopt_ms(push, NNG_OPT_RECONNMINT, 10));
TEST_NNG_PASS(nng_dial(push, addr, NULL, NNG_FLAG_NONBLOCK));

// Start up the dialer first. It should keep retrying every 10 ms.

// Wait 500 milliseconds. This gives a chance for an exponential
// back-off to increase to a longer time. It should by this point
// be well over 100 and probably closer to 200 ms.
nng_msleep(500);

start = nng_clock();
TEST_NNG_PASS(nng_listen(pull, addr, NULL, 0));

TEST_NNG_SEND_STR(push, "hello");
TEST_NNG_RECV_STR(pull, "hello");

TEST_CHECK(nng_clock() - start < 100);

TEST_NNG_PASS(nng_close(push));
TEST_NNG_PASS(nng_close(pull));
}

TEST_LIST = {
{ "dial before listen", test_dial_before_listen },
{ "reconnect", test_reconnect },
{ "reconnect back-off zero", test_reconnect_back_off_zero },
{ "reconnect pipe", test_reconnect_pipe },
{ NULL, NULL },
};

0 comments on commit a052c50

Please sign in to comment.