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 @@ ...@@ -25,18 +25,6 @@
namespace folly { 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. * Thread-safe (atomic) token bucket implementation.
* *
...@@ -54,10 +42,12 @@ struct DefaultTokenBucketClock { ...@@ -54,10 +42,12 @@ struct DefaultTokenBucketClock {
* The "dynamic" base variant allows the token generation rate and maximum * The "dynamic" base variant allows the token generation rate and maximum
* burst size to change with every token consumption. * 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> template <typename Clock = std::chrono::steady_clock>
class ParameterizedDynamicTokenBucket { class BasicDynamicTokenBucket {
static_assert(Clock::is_steady, "clock must be steady");
public: public:
/** /**
* Constructor. * Constructor.
...@@ -66,7 +56,7 @@ class ParameterizedDynamicTokenBucket { ...@@ -66,7 +56,7 @@ class ParameterizedDynamicTokenBucket {
* starting to fill. Defaults to 0, so by default token * starting to fill. Defaults to 0, so by default token
* buckets are "full" after construction. * buckets are "full" after construction.
*/ */
explicit ParameterizedDynamicTokenBucket(double zeroTime = 0) noexcept explicit BasicDynamicTokenBucket(double zeroTime = 0) noexcept
: zeroTime_(zeroTime) {} : zeroTime_(zeroTime) {}
/** /**
...@@ -75,8 +65,7 @@ class ParameterizedDynamicTokenBucket { ...@@ -75,8 +65,7 @@ class ParameterizedDynamicTokenBucket {
* Thread-safe. (Copy constructors of derived classes may not be thread-safe * Thread-safe. (Copy constructors of derived classes may not be thread-safe
* however.) * however.)
*/ */
ParameterizedDynamicTokenBucket( BasicDynamicTokenBucket(const BasicDynamicTokenBucket& other) noexcept
const ParameterizedDynamicTokenBucket& other) noexcept
: zeroTime_(other.zeroTime_.load()) {} : zeroTime_(other.zeroTime_.load()) {}
/** /**
...@@ -85,8 +74,8 @@ class ParameterizedDynamicTokenBucket { ...@@ -85,8 +74,8 @@ class ParameterizedDynamicTokenBucket {
* Warning: not thread safe for the object being assigned to (including * Warning: not thread safe for the object being assigned to (including
* self-assignment). Thread-safe for the other object. * self-assignment). Thread-safe for the other object.
*/ */
ParameterizedDynamicTokenBucket& operator=( BasicDynamicTokenBucket& operator=(
const ParameterizedDynamicTokenBucket& other) noexcept { const BasicDynamicTokenBucket& other) noexcept {
zeroTime_ = other.zeroTime_.load(); zeroTime_ = other.zeroTime_.load();
return *this; return *this;
} }
...@@ -107,10 +96,10 @@ class ParameterizedDynamicTokenBucket { ...@@ -107,10 +96,10 @@ class ParameterizedDynamicTokenBucket {
/** /**
* Returns the current time in seconds since Epoch. * Returns the current time in seconds since Epoch.
*/ */
static double defaultClockNow() noexcept(noexcept(ClockT::timeSinceEpoch())) { static double defaultClockNow() noexcept {
return std::chrono::duration_cast<std::chrono::duration<double>>( using dur = std::chrono::duration<double>;
ClockT::timeSinceEpoch()) auto const now = Clock::now().time_since_epoch();
.count(); return std::chrono::duration_cast<dur>(now).count();
} }
/** /**
...@@ -225,13 +214,15 @@ class ParameterizedDynamicTokenBucket { ...@@ -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. * generation rate and a fixed maximum burst size.
*/ */
template <typename ClockT = DefaultTokenBucketClock> template <typename Clock = std::chrono::steady_clock>
class ParameterizedTokenBucket { class BasicTokenBucket {
static_assert(Clock::is_steady, "clock must be steady");
private: private:
using Impl = ParameterizedDynamicTokenBucket<ClockT>; using Impl = BasicDynamicTokenBucket<Clock>;
public: public:
/** /**
...@@ -243,7 +234,7 @@ class ParameterizedTokenBucket { ...@@ -243,7 +234,7 @@ class ParameterizedTokenBucket {
* starting to fill. Defaults to 0, so by default token * starting to fill. Defaults to 0, so by default token
* bucket is "full" after construction. * bucket is "full" after construction.
*/ */
ParameterizedTokenBucket( BasicTokenBucket(
double genRate, double genRate,
double burstSize, double burstSize,
double zeroTime = 0) noexcept double zeroTime = 0) noexcept
...@@ -257,16 +248,14 @@ class ParameterizedTokenBucket { ...@@ -257,16 +248,14 @@ class ParameterizedTokenBucket {
* *
* Warning: not thread safe! * Warning: not thread safe!
*/ */
ParameterizedTokenBucket(const ParameterizedTokenBucket& other) noexcept = BasicTokenBucket(const BasicTokenBucket& other) noexcept = default;
default;
/** /**
* Copy-assignment operator. * Copy-assignment operator.
* *
* Warning: not thread safe! * Warning: not thread safe!
*/ */
ParameterizedTokenBucket& operator=( BasicTokenBucket& operator=(const BasicTokenBucket& other) noexcept = default;
const ParameterizedTokenBucket& other) noexcept = default;
/** /**
* Returns the current time in seconds since Epoch. * Returns the current time in seconds since Epoch.
...@@ -383,6 +372,7 @@ class ParameterizedTokenBucket { ...@@ -383,6 +372,7 @@ class ParameterizedTokenBucket {
double burstSize_; double burstSize_;
}; };
using TokenBucket = ParameterizedTokenBucket<>; using TokenBucket = BasicTokenBucket<>;
using DynamicTokenBucket = ParameterizedDynamicTokenBucket<>; using DynamicTokenBucket = BasicDynamicTokenBucket<>;
} // namespace folly } // 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