Commit 57ebbcd1 authored by Daniel Xu's avatar Daniel Xu Committed by Facebook Github Bot

Use coarse_steady_clock instead of steady_clock

Summary:
IntervalRateLimiter was already operating on millisecond
granularity. coarse_steady_clock offers better performance because
it's on millisecond granularity.

Reviewed By: yfeldblum

Differential Revision: D8245042

fbshipit-source-id: 7ce0e347c0498ff2741adc727577c3bb2ce63979
parent 0865b933
......@@ -19,14 +19,14 @@ namespace folly {
namespace logging {
IntervalRateLimiter::IntervalRateLimiter(
uint64_t maxPerInterval,
std::chrono::steady_clock::duration interval)
clock::duration interval)
: maxPerInterval_{maxPerInterval},
interval_{interval},
timestamp_{std::chrono::steady_clock::now().time_since_epoch().count()} {}
timestamp_{clock::now().time_since_epoch().count()} {}
bool IntervalRateLimiter::checkSlow() {
auto ts = timestamp_.load();
auto now = std::chrono::steady_clock::now().time_since_epoch().count();
auto now = clock::now().time_since_epoch().count();
if (now < (ts + interval_.count())) {
return false;
}
......
......@@ -19,6 +19,8 @@
#include <chrono>
#include <cstdint>
#include <folly/Chrono.h>
namespace folly {
namespace logging {
......@@ -40,9 +42,9 @@ class RateLimiter {
*/
class IntervalRateLimiter : public RateLimiter {
public:
IntervalRateLimiter(
uint64_t maxPerInterval,
std::chrono::steady_clock::duration interval);
using clock = chrono::coarse_steady_clock;
IntervalRateLimiter(uint64_t maxPerInterval, clock::duration interval);
bool check() override final {
auto origCount = count_.fetch_add(1, std::memory_order_acq_rel);
......@@ -56,13 +58,13 @@ class IntervalRateLimiter : public RateLimiter {
bool checkSlow();
const uint64_t maxPerInterval_;
const std::chrono::steady_clock::time_point::duration interval_;
const clock::time_point::duration interval_;
std::atomic<uint64_t> count_{0};
// Ideally timestamp_ would be a
// std::atomic<std::chrono::steady_clock::time_point>, but this does not work
// since time_point's constructor is not noexcept
std::atomic<std::chrono::steady_clock::rep> timestamp_;
// std::atomic<clock::time_point>, but this does not
// work since time_point's constructor is not noexcept
std::atomic<clock::rep> timestamp_;
};
} // namespace logging
} // namespace folly
......@@ -15,17 +15,19 @@
*/
#include <thread>
#include <folly/Chrono.h>
#include <folly/Conv.h>
#include <folly/logging/RateLimiter.h>
#include <folly/portability/GTest.h>
using folly::chrono::coarse_steady_clock;
using folly::logging::IntervalRateLimiter;
using std::chrono::duration_cast;
using namespace std::literals::chrono_literals;
void intervalTest(
uint64_t eventsPerInterval,
std::chrono::steady_clock::duration interval) {
coarse_steady_clock::duration interval) {
SCOPED_TRACE(folly::to<std::string>(
eventsPerInterval,
" events every ",
......
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