nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
token_bucket.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <chrono>
5
6namespace nanommo {
7
8TokenBucket::TokenBucket(int size, std::chrono::steady_clock::duration refill)
9 : size_(size), refill_(refill), tokens_(size) {}
10
11bool TokenBucket::Take(std::chrono::steady_clock::time_point now) {
12 const double refilled =
13 (now - last_) / std::chrono::duration<double>(refill_);
14 tokens_ = std::min(tokens_ + refilled, static_cast<double>(size_));
15 last_ = now;
16 if (tokens_ < 1.0) {
17 return false;
18 }
19 tokens_ -= 1.0;
20 return true;
21}
22
23} // namespace nanommo
TokenBucket(int size, std::chrono::steady_clock::duration refill)
bool Take(std::chrono::steady_clock::time_point now)
Takes a token out, if the bucket has one at this time.
Definition chat.cc:9