Add a minimum value for the refill bytes per period value

Summary: If the user specified a small enough value for the rate limiter's bytes per second, the calculation for the number of refill bytes per period could become zero which would effectively cause the server to hang forever.

Test Plan: Existing tests

Reviewers: sdong, yhchiang, igor

Reviewed By: igor

Subscribers: leveldb, andrewkr, dhruba

Differential Revision: https://reviews.facebook.net/D56631
This commit is contained in:
Jay Edgar 2016-04-12 15:09:28 -07:00
parent dff4c48ede
commit b345b36620
1 changed files with 5 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <algorithm>
#include <atomic> #include <atomic>
#include <deque> #include <deque>
#include "port/port.h" #include "port/port.h"
@ -60,12 +61,15 @@ class GenericRateLimiter : public RateLimiter {
private: private:
void Refill(); void Refill();
int64_t CalculateRefillBytesPerPeriod(int64_t rate_bytes_per_sec) { int64_t CalculateRefillBytesPerPeriod(int64_t rate_bytes_per_sec) {
return rate_bytes_per_sec * refill_period_us_ / 1000000; return std::max(kMinRefillBytesPerPeriod,
rate_bytes_per_sec * refill_period_us_ / 1000000);
} }
// This mutex guard all internal states // This mutex guard all internal states
mutable port::Mutex request_mutex_; mutable port::Mutex request_mutex_;
const int64_t kMinRefillBytesPerPeriod = 100;
const int64_t refill_period_us_; const int64_t refill_period_us_;
// This variable can be changed dynamically. // This variable can be changed dynamically.
std::atomic<int64_t> refill_bytes_per_period_; std::atomic<int64_t> refill_bytes_per_period_;