diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-05-23 22:54:28 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-05-24 11:01:51 -0700 |
| commit | a052c50fe7cd462a206a2f73737c084a20d04aa0 (patch) | |
| tree | 8577adb35adb68a44b05d588115c9ce26ecfe781 | |
| parent | 6f084d8c33e44cc69c7e5f29b6e169d7f34d0e0b (diff) | |
| download | nng-a052c50fe7cd462a206a2f73737c084a20d04aa0.tar.gz nng-a052c50fe7cd462a206a2f73737c084a20d04aa0.tar.bz2 nng-a052c50fe7cd462a206a2f73737c084a20d04aa0.zip | |
fixes #1230 NNG_OPT_RECONNMAXT zero does not prevent exponential back-off
| -rw-r--r-- | src/core/socket.c | 20 | ||||
| -rw-r--r-- | tests/reconnect.c | 40 |
2 files changed, 50 insertions, 10 deletions
diff --git a/src/core/socket.c b/src/core/socket.c index bffe0f67..6e3f14d0 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -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 diff --git a/tests/reconnect.c b/tests/reconnect.c index 0ee63c66..317f1bec 100644 --- a/tests/reconnect.c +++ b/tests/reconnect.c @@ -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" @@ -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); @@ -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 }, };
\ No newline at end of file |
