Commit fb0c95ee authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Tweaks to folly/TokenBucket.h

Summary:
[Folly] Tweaks to `folly/TokenBucket.h`.

* No custom clock wrapper - just use the clock interface directly.
* Rename from `Parameterized` to `Basic`.

Reviewed By: andrewjcg

Differential Revision: D7319361

fbshipit-source-id: 1a7d8761e58f5f9b06dd8fdf5f7916913354c3b9
parent b1da501d
......@@ -25,18 +25,6 @@
namespace folly {
/**
* Default clock class used by ParameterizedDynamicTokenBucket and derived
* classes. User-defined clock classes must be steady (monotonic) and define a
* static function std::chrono::duration<> timeSinceEpoch().
*/
struct DefaultTokenBucketClock {
static auto timeSinceEpoch() noexcept
-> decltype(std::chrono::steady_clock::now().time_since_epoch()) {
return std::chrono::steady_clock::now().time_since_epoch();
}
};
/**
* Thread-safe (atomic) token bucket implementation.
*
......@@ -54,10 +42,12 @@ struct DefaultTokenBucketClock {
* The "dynamic" base variant allows the token generation rate and maximum
* burst size to change with every token consumption.
*
* @tparam ClockT Clock type, must be steady i.e. monotonic.
* @tparam Clock Clock type, must be steady i.e. monotonic.
*/
template <typename ClockT = DefaultTokenBucketClock>
class ParameterizedDynamicTokenBucket {
template <typename Clock = std::chrono::steady_clock>
class BasicDynamicTokenBucket {
static_assert(Clock::is_steady, "clock must be steady");
public:
/**
* Constructor.
......@@ -66,7 +56,7 @@ class ParameterizedDynamicTokenBucket {
* starting to fill. Defaults to 0, so by default token
* buckets are "full" after construction.
*/
explicit ParameterizedDynamicTokenBucket(double zeroTime = 0) noexcept
explicit BasicDynamicTokenBucket(double zeroTime = 0) noexcept
: zeroTime_(zeroTime) {}
/**
......@@ -75,8 +65,7 @@ class ParameterizedDynamicTokenBucket {
* Thread-safe. (Copy constructors of derived classes may not be thread-safe
* however.)
*/
ParameterizedDynamicTokenBucket(
const ParameterizedDynamicTokenBucket& other) noexcept
BasicDynamicTokenBucket(const BasicDynamicTokenBucket& other) noexcept
: zeroTime_(other.zeroTime_.load()) {}
/**
......@@ -85,8 +74,8 @@ class ParameterizedDynamicTokenBucket {
* Warning: not thread safe for the object being assigned to (including
* self-assignment). Thread-safe for the other object.
*/
ParameterizedDynamicTokenBucket& operator=(
const ParameterizedDynamicTokenBucket& other) noexcept {
BasicDynamicTokenBucket& operator=(
const BasicDynamicTokenBucket& other) noexcept {
zeroTime_ = other.zeroTime_.load();
return *this;
}
......@@ -107,10 +96,10 @@ class ParameterizedDynamicTokenBucket {
/**
* Returns the current time in seconds since Epoch.
*/
static double defaultClockNow() noexcept(noexcept(ClockT::timeSinceEpoch())) {
return std::chrono::duration_cast<std::chrono::duration<double>>(
ClockT::timeSinceEpoch())
.count();
static double defaultClockNow() noexcept {
using dur = std::chrono::duration<double>;
auto const now = Clock::now().time_since_epoch();
return std::chrono::duration_cast<dur>(now).count();
}
/**
......@@ -225,13 +214,15 @@ class ParameterizedDynamicTokenBucket {
};
/**
* Specialization of ParameterizedDynamicTokenBucket with a fixed token
* Specialization of BasicDynamicTokenBucket with a fixed token
* generation rate and a fixed maximum burst size.
*/
template <typename ClockT = DefaultTokenBucketClock>
class ParameterizedTokenBucket {
template <typename Clock = std::chrono::steady_clock>
class BasicTokenBucket {
static_assert(Clock::is_steady, "clock must be steady");
private:
using Impl = ParameterizedDynamicTokenBucket<ClockT>;
using Impl = BasicDynamicTokenBucket<Clock>;
public:
/**
......@@ -243,7 +234,7 @@ class ParameterizedTokenBucket {
* starting to fill. Defaults to 0, so by default token
* bucket is "full" after construction.
*/
ParameterizedTokenBucket(
BasicTokenBucket(
double genRate,
double burstSize,
double zeroTime = 0) noexcept
......@@ -257,16 +248,14 @@ class ParameterizedTokenBucket {
*
* Warning: not thread safe!
*/
ParameterizedTokenBucket(const ParameterizedTokenBucket& other) noexcept =
default;
BasicTokenBucket(const BasicTokenBucket& other) noexcept = default;
/**
* Copy-assignment operator.
*
* Warning: not thread safe!
*/
ParameterizedTokenBucket& operator=(
const ParameterizedTokenBucket& other) noexcept = default;
BasicTokenBucket& operator=(const BasicTokenBucket& other) noexcept = default;
/**
* Returns the current time in seconds since Epoch.
......@@ -383,6 +372,7 @@ class ParameterizedTokenBucket {
double burstSize_;
};
using TokenBucket = ParameterizedTokenBucket<>;
using DynamicTokenBucket = ParameterizedDynamicTokenBucket<>;
using TokenBucket = BasicTokenBucket<>;
using DynamicTokenBucket = BasicDynamicTokenBucket<>;
} // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment