Improve update interval calculation

This commit is contained in:
King_DuckZ 2020-09-05 02:49:09 +01:00
parent 200c74757d
commit 8be26c2d1c

View file

@ -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<double>(header.rate_limit_remaining);
const auto& retry_after = header.retry_after;
const double ra = static_cast<double>(retry_after);
const auto wait = static_cast<unsigned long>(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) {