Skip to content
Permalink
Browse files

rate limiter: cleanup

  • Loading branch information...
aberaud committed Nov 8, 2019
1 parent cb7d7f6 commit cdd035b8014e8e74a6a8e00fd4b390fe502b3058
Showing with 6 additions and 8 deletions.
  1. +1 −1 include/opendht/network_engine.h
  2. +5 −7 include/opendht/rate_limiter.h
@@ -534,7 +534,7 @@ class NetworkEngine final
using IpLimiterMap = std::map<SockAddr, IpLimiter, SockAddr::ipCmp>;
IpLimiterMap address_rate_limiter;
RateLimiter rate_limiter;
size_t limiter_maintenance {0};
ssize_t limiter_maintenance {0};

// requests handling
std::map<Tid, Sp<Request>> requests {};
@@ -25,19 +25,19 @@ namespace dht {

class RateLimiter {
public:
RateLimiter() = delete;
RateLimiter(size_t quota) : quota_(quota) {}
RateLimiter(size_t quota, const duration& period = std::chrono::seconds(1))
: quota_(quota), period_(period) {}

/** Clear outdated records and return current quota usage */
size_t maintain(const time_point& now) {
auto limit = now - std::chrono::seconds(1);
auto limit = now - period_;
while (not records.empty() and records.front() < limit)
records.pop();
return records.size();
}
/** Return false if quota is reached, insert record and return true otherwise. */
bool limit(const time_point& now) {
if (quota_ == (size_t)-1)
if (quota_ == std::numeric_limits<size_t>::max())
return true;
if (maintain(now) >= quota_)
return false;
@@ -47,11 +47,9 @@ class RateLimiter {
bool empty() const {
return records.empty();
}
size_t quota() const {
return quota_;
}
private:
const size_t quota_;
const duration period_;
std::queue<time_point> records {};
};

0 comments on commit cdd035b

Please sign in to comment.
You can’t perform that action at this time.