]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
pinger: improve timer accuracy and resolution (#1277)
authorAmos Jeffries <yadij@users.noreply.github.com>
Sat, 25 Feb 2023 16:28:14 +0000 (16:28 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Sat, 25 Feb 2023 16:28:23 +0000 (16:28 +0000)
For consistency across platforms, we also increase the ICMP request
timeout on Windows systems from 5 to 10 seconds.

src/icmp/pinger.cc

index 9b06b08fd2209b4bbc1a062e7435d758d03849cf..67a717da5a7a9b535f3111849e274758b0c26e53 100644 (file)
@@ -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<std::chrono::seconds>(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;
         }
     }