From: Remi Gacogne Date: Tue, 24 Jan 2023 10:16:18 +0000 (+0100) Subject: dnsdist: Use normalizeTV() and timeval operator+/operator- X-Git-Tag: dnsdist-1.8.0-rc1~86^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F12388%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Use normalizeTV() and timeval operator+/operator- As suggested by Otto (thanks!). --- diff --git a/pdns/dnsdistdist/dnsdist-async.cc b/pdns/dnsdistdist/dnsdist-async.cc index fc9174401c..981464074a 100644 --- a/pdns/dnsdistdist/dnsdist-async.cc +++ b/pdns/dnsdistdist/dnsdist-async.cc @@ -387,10 +387,7 @@ bool suspendQuery(DNSQuestion& dq, uint16_t asyncID, uint16_t queryID, uint32_t struct timeval ttd = now; ttd.tv_sec += timeoutMs / 1000; ttd.tv_usec += (timeoutMs % 1000) * 1000; - if (ttd.tv_usec >= 1000000) { - ttd.tv_sec++; - ttd.tv_usec -= 1000000; - } + normalizeTV(ttd); vinfolog("Suspending asynchronous query %d at %d.%d until %d.%d", queryID, now.tv_sec, now.tv_usec, ttd.tv_sec, ttd.tv_usec); auto query = getInternalQueryFromDQ(dq, false); @@ -410,10 +407,7 @@ bool suspendResponse(DNSResponse& dr, uint16_t asyncID, uint16_t queryID, uint32 struct timeval ttd = now; ttd.tv_sec += timeoutMs / 1000; ttd.tv_usec += (timeoutMs % 1000) * 1000; - if (ttd.tv_usec >= 1000000) { - ttd.tv_sec++; - ttd.tv_usec -= 1000000; - } + normalizeTV(ttd); vinfolog("Suspending asynchronous response %d at %d.%d until %d.%d", queryID, now.tv_sec, now.tv_usec, ttd.tv_sec, ttd.tv_usec); auto query = getInternalQueryFromDQ(dr, true); diff --git a/pdns/dnsdistdist/dnsdist-healthchecks.cc b/pdns/dnsdistdist/dnsdist-healthchecks.cc index 480bfd1960..3f8555f35b 100644 --- a/pdns/dnsdistdist/dnsdist-healthchecks.cc +++ b/pdns/dnsdistdist/dnsdist-healthchecks.cc @@ -334,10 +334,7 @@ bool queueHealthCheck(std::unique_ptr& mplexer, const std::shared gettimeofday(&data->d_ttd, nullptr); data->d_ttd.tv_sec += ds->d_config.checkTimeout / 1000; /* ms to seconds */ data->d_ttd.tv_usec += (ds->d_config.checkTimeout % 1000) * 1000; /* remaining ms to us */ - if (data->d_ttd.tv_usec > 1000000) { - ++data->d_ttd.tv_sec; - data->d_ttd.tv_usec -= 1000000; - } + normalizeTV(data->d_ttd); if (!ds->doHealthcheckOverTCP()) { sock.connect(ds->d_config.remote); diff --git a/pdns/dnsdistdist/dnsdist-tcp-downstream.hh b/pdns/dnsdistdist/dnsdist-tcp-downstream.hh index c966f823c7..81c87570b5 100644 --- a/pdns/dnsdistdist/dnsdist-tcp-downstream.hh +++ b/pdns/dnsdistdist/dnsdist-tcp-downstream.hh @@ -157,6 +157,7 @@ protected: struct timeval res = now; res.tv_sec += d_ds->d_config.checkTimeout / 1000; /* ms to s */ res.tv_usec += (d_ds->d_config.checkTimeout % 1000) * 1000; /* remaining ms to µs */ + normalizeTV(res); return res; } diff --git a/pdns/dnsdistdist/test-dnsdistasync.cc b/pdns/dnsdistdist/test-dnsdistasync.cc index 2e35d3679f..802ba4021d 100644 --- a/pdns/dnsdistdist/test-dnsdistasync.cc +++ b/pdns/dnsdistdist/test-dnsdistasync.cc @@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(test_Basic) gettimeofday(&ttd, nullptr); // timeout in 100 ms const timeval add{0, 100000}; - timeradd(&ttd, &add, &ttd); + ttd = ttd + add; holder->push(asyncID, queryID, ttd, std::make_unique()); BOOST_CHECK(!holder->empty()); @@ -116,7 +116,7 @@ BOOST_AUTO_TEST_CASE(test_TimeoutFailClose) gettimeofday(&ttd, nullptr); // timeout in 10 ms const timeval add{0, 10000}; - timeradd(&ttd, &add, &ttd); + ttd = ttd + add; std::shared_ptr sender{nullptr}; { @@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE(test_AddingExpiredEvent) gettimeofday(&ttd, nullptr); // timeout was 10 ms ago, for some reason (long processing time, CPU starvation...) const timeval sub{0, 10000}; - timersub(&ttd, &sub, &ttd); + ttd = ttd - sub; std::shared_ptr sender{nullptr}; {