From 0d51e000cc784a5458f72d19cf38400479f0df9b Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Sat, 25 Feb 2023 16:28:14 +0000 Subject: [PATCH] pinger: improve timer accuracy and resolution (#1277) For consistency across platforms, we also increase the ICMP request timeout on Windows systems from 5 to 10 seconds. --- src/icmp/pinger.cc | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/icmp/pinger.cc b/src/icmp/pinger.cc index 9b06b08fd2..67a717da5a 100644 --- a/src/icmp/pinger.cc +++ b/src/icmp/pinger.cc @@ -44,6 +44,7 @@ #if USE_ICMP +#include "base/Stopwatch.h" #include "Icmp4.h" #include "Icmp6.h" #include "IcmpPinger.h" @@ -65,8 +66,6 @@ #include "fde.h" -#define PINGER_TIMEOUT 5 - /* windows uses the control socket for feedback to squid */ #define LINK_TO_SQUID squid_link @@ -84,13 +83,14 @@ Win32__WSAFDIsSet(int fd, fd_set FAR * set) #else -#define PINGER_TIMEOUT 10 - /* non-windows use STDOUT for feedback to squid */ #define LINK_TO_SQUID 1 #endif /* _SQUID_WINDOWS_ */ +using namespace std::literals::chrono_literals; +static const auto PingerTimeout = 10s; + // ICMP Engines are declared global here so they can call each other easily. IcmpPinger control; Icmp4 icmp4; @@ -109,9 +109,6 @@ main(int, char **) int x; int max_fd = 0; - struct timeval tv; - time_t last_check_time = 0; - /* * cevans - do this first. It grabs a raw socket. After this we can * drop privs @@ -198,10 +195,9 @@ main(int, char **) } #endif - last_check_time = squid_curtime; - for (;;) { - tv.tv_sec = PINGER_TIMEOUT; + struct timeval tv; + tv.tv_sec = std::chrono::seconds(PingerTimeout).count(); tv.tv_usec = 0; FD_ZERO(&R); if (icmp4_worker >= 0) { @@ -212,6 +208,8 @@ main(int, char **) } FD_SET(squid_link, &R); + Stopwatch timer; + timer.resume(); x = select(max_fd+1, &R, nullptr, nullptr, &tv); getCurrentTime(); @@ -233,14 +231,13 @@ main(int, char **) icmp4.Recv(); } - if (PINGER_TIMEOUT + last_check_time < squid_curtime) { + const auto delay = std::chrono::duration_cast(timer.total()); + if (delay >= PingerTimeout) { if (send(LINK_TO_SQUID, &tv, 0, 0) < 0) { - debugs(42, DBG_CRITICAL, "Closing. No requests in last " << PINGER_TIMEOUT << " seconds."); + debugs(42, DBG_CRITICAL, "Closing. No requests in last " << delay.count() << " seconds."); control.Close(); exit(EXIT_FAILURE); } - - last_check_time = squid_curtime; } } -- 2.47.2