Skip to content

Commit dcd74b5

Browse files
authored
Merge pull request #744 from oko256/reap-interval
Adjust reap interval based on timeouts
2 parents 0fdb8c4 + d3a6d5e commit dcd74b5

2 files changed

Lines changed: 76 additions & 9 deletions

File tree

src/zyre.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,64 @@ zyre_test (bool verbose)
890890
zstr_free (&command);
891891
zmsg_destroy (&msg);
892892

893+
// First node should also receive ENTER and JOINs from second node
894+
msg = zyre_recv (node1);
895+
assert (msg);
896+
command = zmsg_popstr (msg);
897+
assert (streq (command, "ENTER"));
898+
zstr_free (&command);
899+
assert (zmsg_size (msg) == 4);
900+
peerid = zmsg_popstr (msg);
901+
name = zmsg_popstr (msg);
902+
assert (streq (name, "node2"));
903+
zstr_free (&peerid);
904+
zstr_free (&name);
905+
zmsg_destroy (&msg);
906+
907+
msg = zyre_recv (node1);
908+
assert (msg);
909+
command = zmsg_popstr (msg);
910+
assert (streq (command, "JOIN"));
911+
zstr_free (&command);
912+
assert (zmsg_size (msg) == 3);
913+
zmsg_destroy (&msg);
914+
915+
msg = zyre_recv (node1);
916+
assert (msg);
917+
command = zmsg_popstr (msg);
918+
assert (streq (command, "JOIN"));
919+
zstr_free (&command);
920+
assert (zmsg_size (msg) == 3);
921+
zmsg_destroy (&msg);
922+
923+
// Test evasive timeout
924+
const int evasive_test_interval = 100;
925+
zyre_set_evasive_timeout (node1, evasive_test_interval);
926+
// Refresh peers to apply new timeouts immediately
927+
zyre_shouts (node1, "GLOBAL", "Hello again");
928+
zyre_shouts (node2, "GLOBAL", "Hello again");
929+
msg = zyre_recv (node1);
930+
assert (msg);
931+
command = zmsg_popstr (msg);
932+
assert (streq (command, "SHOUT"));
933+
zstr_free (&command);
934+
zmsg_destroy (&msg);
935+
msg = zyre_recv (node2);
936+
assert (msg);
937+
command = zmsg_popstr (msg);
938+
assert (streq (command, "SHOUT"));
939+
zstr_free (&command);
940+
zmsg_destroy (&msg);
941+
942+
int64_t recv_start = zclock_mono ();
943+
msg = zyre_recv (node1);
944+
assert ((zclock_mono () - recv_start) < (evasive_test_interval + 100));
945+
assert (msg);
946+
command = zmsg_popstr (msg);
947+
assert (streq (command, "EVASIVE"));
948+
zstr_free (&command);
949+
zmsg_destroy (&msg);
950+
893951
zyre_stop (node2);
894952

895953
msg = zyre_recv (node2);

src/zyre_node.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ s_string_compare (void *item1, void *item2)
9191
return strcmp (str1, str2);
9292
}
9393

94+
static int64_t
95+
s_reap_interval (zyre_node_t *self)
96+
{
97+
uint64_t interval = self->evasive_timeout;
98+
if (self->expired_timeout < interval)
99+
interval = self->expired_timeout;
100+
if (interval > REAP_INTERVAL)
101+
interval = REAP_INTERVAL;
102+
return interval;
103+
}
104+
94105
// --------------------------------------------------------------------------
95106
// Constructor
96107

@@ -1547,15 +1558,15 @@ zyre_node_ping_peer (const char *key, void *item, void *argument)
15471558
zstr_sendm (self->outbox, "EVASIVE");
15481559
zstr_sendm (self->outbox, zyre_peer_identity (peer));
15491560
zstr_send (self->outbox, zyre_peer_name (peer));
1550-
if (zclock_mono () >= zyre_peer_evasive_at (peer) + REAP_INTERVAL) {
1561+
if (zclock_mono () >= zyre_peer_evasive_at (peer) + s_reap_interval (self)) {
15511562
// Inform the calling application this peer is being silent
15521563
// despite having tried to ping it. Something is wrong with
15531564
// the connection to this peer (or with the network).
15541565
// NB: this is an improvement of the EVASIVE event which triggers
15551566
// before getting ping result and thus has poor meaning.
15561567
if (self->verbose)
15571568
zsys_info ("(%s) peer '%s' has not answered ping after %d milliseconds (silent)",
1558-
self->name, zyre_peer_name(peer), REAP_INTERVAL);
1569+
self->name, zyre_peer_name(peer), s_reap_interval (self));
15591570
zstr_sendm (self->outbox, "SILENT");
15601571
zstr_sendm (self->outbox, zyre_peer_identity (peer));
15611572
zstr_send (self->outbox, zyre_peer_name (peer));
@@ -1581,7 +1592,7 @@ zyre_node_actor (zsock_t *pipe, void *args)
15811592
zsock_signal (self->pipe, 0);
15821593

15831594
// Loop until the agent is terminated one way or another
1584-
int64_t reap_at = zclock_mono () + REAP_INTERVAL;
1595+
int64_t last_reaped_at = zclock_mono ();
15851596
while (!self->terminated) {
15861597

15871598
// Start beacon as soon as we can
@@ -1635,10 +1646,8 @@ zyre_node_actor (zsock_t *pipe, void *args)
16351646
zstr_free(&hostname);
16361647
}
16371648

1638-
int timeout = (int) (reap_at - zclock_mono ());
1639-
if (timeout > REAP_INTERVAL)
1640-
timeout = REAP_INTERVAL;
1641-
else
1649+
// If nothing else happens, wait until the next reap
1650+
int timeout = (int) ((last_reaped_at + s_reap_interval (self)) - zclock_mono ());
16421651
if (timeout < 0)
16431652
timeout = 0;
16441653

@@ -1661,9 +1670,9 @@ zyre_node_actor (zsock_t *pipe, void *args)
16611670
break; // Interrupted, check before expired
16621671
else
16631672
if (zpoller_expired (self->poller)) {
1664-
if (zclock_mono () >= reap_at) {
1673+
if (zclock_mono () >= (last_reaped_at + s_reap_interval (self))) {
16651674
void *item;
1666-
reap_at = zclock_mono () + REAP_INTERVAL;
1675+
last_reaped_at = zclock_mono ();
16671676
// Ping all peers and reap any expired ones
16681677
for (item = zhash_first (self->peers); item != NULL;
16691678
item = zhash_next (self->peers))

0 commit comments

Comments
 (0)