From b345b36620c9cbdd30d04c23c632da5e382703b9 Mon Sep 17 00:00:00 2001 From: Jay Edgar Date: Tue, 12 Apr 2016 15:09:28 -0700 Subject: [PATCH] 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 --- util/rate_limiter.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/util/rate_limiter.h b/util/rate_limiter.h index d413d9c6e..ea2975d64 100644 --- a/util/rate_limiter.h +++ b/util/rate_limiter.h @@ -9,6 +9,7 @@ #pragma once +#include #include #include #include "port/port.h" @@ -60,12 +61,15 @@ class GenericRateLimiter : public RateLimiter { private: void Refill(); 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 mutable port::Mutex request_mutex_; + const int64_t kMinRefillBytesPerPeriod = 100; + const int64_t refill_period_us_; // This variable can be changed dynamically. std::atomic refill_bytes_per_period_;