#include "rate_limiter.hpp" #include namespace svapi { RateLimiter::RateLimiter() : now_([]{ return std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); }) {} bool RateLimiter::allow(const std::string& keyId, uint32_t limitPerMin) { if (limitPerMin == 0) return true; std::lock_guard lk(mu_); int64_t minute = now_() / 60; auto& w = windows_[keyId]; if (w.minute != minute) { w.minute = minute; w.count = 0; } if (w.count >= limitPerMin) return false; ++w.count; return true; } void RateLimiter::setClockForTesting(std::function fn) { std::lock_guard lk(mu_); now_ = std::move(fn); } RateLimiter& rateLimiter() { static RateLimiter inst; return inst; } }