From 8be26c2d1c1cb98a6ed032df476a927a01d9fb5d Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 5 Sep 2020 02:49:09 +0100 Subject: [PATCH] Improve update interval calculation --- src/timer_base.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/timer_base.cpp b/src/timer_base.cpp index d29dd6f..814ff4c 100644 --- a/src/timer_base.cpp +++ b/src/timer_base.cpp @@ -28,7 +28,20 @@ namespace duck { namespace { [[gnu::pure]] unsigned long time_interval (const oro::Header& header, double min_wait, double extra) { - return std::max(header.retry_after / header.rate_limit + extra, min_wait); + //The way I understand retry_after as returned by the origins server is + //that it's just how long you need to wait before all your retries + //reset to the max. When you get for example to 5/8 retries, even if you + //wait some time that number will never go back to 6. At some point it + //will just resent to 8. You can do as many as 8 calls one after the + //other too, it simply means that until the counter resets back to 8 you + //just won't be able to do anything else. + const double rem = static_cast(header.rate_limit_remaining); + const auto& retry_after = header.retry_after; + const double ra = static_cast(retry_after); + + const auto wait = static_cast(ra / (rem + 1.0) + 0.5); + const auto extra_sec = (wait == retry_after ? 1UL : 0UL); + return std::max(wait + extra_sec + extra, min_wait); } oro::Timestamp calc_next_update (const oro::Header& header, double min_wait, double extra) {