From a089e8ae7504e55e05b1a88c3d8d1327460b8d4f Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 16 Sep 2022 20:18:33 +0200 Subject: [PATCH] pex: avoid sending a query to a host more than once every 15 seconds Avoids unnecessary packet spam Signed-off-by: Felix Fietkau --- host.h | 2 ++ pex.c | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/host.h b/host.h index b3fef03..cb4085e 100644 --- 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 b228576..0707ef0 100644 --- 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); -- 2.30.2