pex: avoid sending a query to a host more than once every 15 seconds
authorFelix Fietkau <nbd@nbd.name>
Fri, 16 Sep 2022 18:18:33 +0000 (20:18 +0200)
committerFelix Fietkau <nbd@nbd.name>
Fri, 16 Sep 2022 19:06:48 +0000 (21:06 +0200)
Avoids unnecessary packet spam

Signed-off-by: Felix Fietkau <nbd@nbd.name>
host.h
pex.c

diff --git a/host.h b/host.h
index b3fef037e8596196839ed8eb28337ff440b1189a..cb4085eecf3fe9ab3c58ed836e682a13715c6d97 100644 (file)
--- a/host.h
+++ b/host.h
@@ -40,6 +40,8 @@ struct network_peer {
                uint64_t rx_bytes;
                uint64_t last_handshake;
                uint64_t last_request;
+               uint64_t last_query_sent;
+
                int idle;
                int num_net_queries;
        } state;
diff --git a/pex.c b/pex.c
index b2285767f4f5dcda22d6d290a39b62ec726db447..0707ef0ddbd5e8bcfda00c52ddef3c8a428c9a54 100644 (file)
--- a/pex.c
+++ b/pex.c
@@ -330,6 +330,7 @@ static void
 network_pex_query_hosts(struct network *net)
 {
        struct network_host *host;
+       uint64_t now;
        int rv = rand();
        int hosts = 0;
        int i;
@@ -355,6 +356,7 @@ network_pex_query_hosts(struct network *net)
        if (!hosts)
                return;
 
+       now = unet_gettime();
        rv %= net->hosts.count;
        for (i = 0; i < 2; i++) {
                avl_for_each_element(&net->hosts, host, node) {
@@ -368,7 +370,8 @@ network_pex_query_hosts(struct network *net)
                        if (host == net->net_config.local_host)
                                continue;
 
-                       if (!peer->state.connected)
+                       if (!peer->state.connected ||
+                           peer->state.last_query_sent + 15 >= now)
                                continue;
 
                        D_PEER(net, peer, "send query for %d hosts", hosts);
@@ -421,6 +424,7 @@ void network_pex_event(struct network *net, struct network_peer *peer,
 
        switch (ev) {
        case PEX_EV_HANDSHAKE:
+               peer->state.last_query_sent = 0;
                pex_send_hello(net, peer);
                if (net->config.type == NETWORK_TYPE_DYNAMIC)
                        network_pex_send_update_request(net, peer, NULL);