nanoMMO
0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
token_bucket.cc
Go to the documentation of this file.
1
#include "
server/token_bucket.h
"
2
3
#include <algorithm>
4
#include <chrono>
5
6
namespace
nanommo
{
7
8
TokenBucket::TokenBucket
(
int
size, std::chrono::steady_clock::duration refill)
9
: size_(size), refill_(refill), tokens_(size) {}
10
11
bool
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
nanommo::TokenBucket::TokenBucket
TokenBucket(int size, std::chrono::steady_clock::duration refill)
Definition
token_bucket.cc:8
nanommo::TokenBucket::Take
bool Take(std::chrono::steady_clock::time_point now)
Takes a token out, if the bucket has one at this time.
Definition
token_bucket.cc:11
nanommo
Definition
chat.cc:9
token_bucket.h
server
token_bucket.cc
Generated by
1.15.0