2025-02-01 11:04:33 -08:00
|
|
|
#include "myirc/ratelimit.hpp"
|
|
|
|
|
2025-01-31 16:14:13 -08:00
|
|
|
#include <chrono>
|
|
|
|
|
2025-02-01 11:04:33 -08:00
|
|
|
namespace myirc {
|
|
|
|
|
2025-01-31 16:14:13 -08:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
}
|
2025-02-01 11:04:33 -08:00
|
|
|
|
|
|
|
} // namespace myirc
|