proxygen
folly::logging::IntervalRateLimiter Class Reference

#include <RateLimiter.h>

Public Types

using clock = chrono::coarse_steady_clock
 

Public Member Functions

constexpr IntervalRateLimiter (uint64_t maxPerInterval, clock::duration interval)
 
bool check ()
 

Private Member Functions

bool checkSlow ()
 

Private Attributes

const uint64_t maxPerInterval_
 
const clock::time_point::duration interval_
 
std::atomic< uint64_tcount_ {std::numeric_limits<uint64_t>::max()}
 
std::atomic< clock::reptimestamp_ {0}
 

Detailed Description

A rate limiter that can rate limit events to N events per M milliseconds.

It is intended to be fast to check when messages are not being rate limited. When messages are being rate limited it is slightly slower, as it has to check the clock each time check() is called in this case.

Definition at line 34 of file RateLimiter.h.

Member Typedef Documentation

Constructor & Destructor Documentation

constexpr folly::logging::IntervalRateLimiter::IntervalRateLimiter ( uint64_t  maxPerInterval,
clock::duration  interval 
)
inline

Definition at line 38 of file RateLimiter.h.

References interval_.

41  : maxPerInterval_{maxPerInterval}, interval_{interval} {}
const clock::time_point::duration interval_
Definition: RateLimiter.h:55

Member Function Documentation

bool folly::logging::IntervalRateLimiter::check ( )
inline

Definition at line 43 of file RateLimiter.h.

References checkSlow(), count_, and maxPerInterval_.

43  {
44  auto origCount = count_.fetch_add(1, std::memory_order_acq_rel);
45  if (origCount < maxPerInterval_) {
46  return true;
47  }
48  return checkSlow();
49  }
std::atomic< uint64_t > count_
Definition: RateLimiter.h:60
bool folly::logging::IntervalRateLimiter::checkSlow ( )
private

Definition at line 21 of file RateLimiter.cpp.

References count_, interval_, maxPerInterval_, now(), folly::chrono::coarse_steady_clock::now(), and timestamp_.

Referenced by check().

21  {
22  auto ts = timestamp_.load();
23  auto now = clock::now().time_since_epoch().count();
24  if (now < (ts + interval_.count())) {
25  return false;
26  }
27 
28  if (!timestamp_.compare_exchange_strong(ts, now)) {
29  // We raced with another thread that reset the timestamp.
30  // We treat this as if we fell into the previous interval, and so we
31  // rate-limit ourself.
32  return false;
33  }
34 
35  if (ts == 0) {
36  // If we initialized timestamp_ for the very first time increment count_ by
37  // one instead of setting it to 0. Our original increment made it roll over
38  // to 0, so other threads may have already incremented it again and passed
39  // the check.
40  auto origCount = count_.fetch_add(1, std::memory_order_acq_rel);
41  // Check to see if other threads already hit the rate limit cap before we
42  // finished checkSlow().
43  return (origCount < maxPerInterval_);
44  }
45 
46  // In the future, if we wanted to return the number of dropped events we
47  // could use (count_.exchange(0) - maxPerInterval_) here.
48  count_.store(1, std::memory_order_release);
49  return true;
50 }
std::atomic< clock::rep > timestamp_
Definition: RateLimiter.h:64
const clock::time_point::duration interval_
Definition: RateLimiter.h:55
std::chrono::steady_clock::time_point now()
std::atomic< uint64_t > count_
Definition: RateLimiter.h:60
static time_point now() noexcept
Definition: Chrono.h:171

Member Data Documentation

std::atomic<uint64_t> folly::logging::IntervalRateLimiter::count_ {std::numeric_limits<uint64_t>::max()}
private

Definition at line 60 of file RateLimiter.h.

Referenced by check(), and checkSlow().

const clock::time_point::duration folly::logging::IntervalRateLimiter::interval_
private

Definition at line 55 of file RateLimiter.h.

Referenced by checkSlow(), and IntervalRateLimiter().

const uint64_t folly::logging::IntervalRateLimiter::maxPerInterval_
private

Definition at line 54 of file RateLimiter.h.

Referenced by check(), and checkSlow().

std::atomic<clock::rep> folly::logging::IntervalRateLimiter::timestamp_ {0}
private

Definition at line 64 of file RateLimiter.h.

Referenced by checkSlow().


The documentation for this class was generated from the following files: