24 lines
595 B
C++
24 lines
595 B
C++
|
#include "ratelimit.hpp"
|
||
|
#include <chrono>
|
||
|
|
||
|
using namespace std::literals;
|
||
|
using ms = std::chrono::milliseconds;
|
||
|
|
||
|
auto Rfc1459RateLimit::query(size_t want_to_send) -> std::pair<ms, size_t>
|
||
|
{
|
||
|
const auto now = clock::now();
|
||
|
if (horizon_ < now) horizon_ = now;
|
||
|
|
||
|
auto gap = std::chrono::floor<ms>(now + allowance_ - horizon_);
|
||
|
auto send = gap / cost_;
|
||
|
if (std::cmp_greater(send, want_to_send)) send = want_to_send;
|
||
|
|
||
|
if (send > 0) {
|
||
|
horizon_ += send * cost_;
|
||
|
return {0ms, send};
|
||
|
} else {
|
||
|
horizon_ += cost_;
|
||
|
return {cost_ - gap, 1};
|
||
|
}
|
||
|
}
|