@ -36,7 +36,7 @@ GenericRateLimiter::GenericRateLimiter(int64_t rate_bytes_per_sec,
exit_cv_ ( & request_mutex_ ) ,
exit_cv_ ( & request_mutex_ ) ,
requests_to_wait_ ( 0 ) ,
requests_to_wait_ ( 0 ) ,
available_bytes_ ( 0 ) ,
available_bytes_ ( 0 ) ,
next_refill_us_ ( env_ - > NowMicros ( ) ) ,
next_refill_us_ ( NowMicrosMonotonic ( env_ ) ) ,
fairness_ ( fairness > 100 ? 100 : fairness ) ,
fairness_ ( fairness > 100 ? 100 : fairness ) ,
rnd_ ( ( uint32_t ) time ( nullptr ) ) ,
rnd_ ( ( uint32_t ) time ( nullptr ) ) ,
leader_ ( nullptr ) {
leader_ ( nullptr ) {
@ -107,7 +107,14 @@ void GenericRateLimiter::Request(int64_t bytes, const Env::IOPriority pri) {
( ! queue_ [ Env : : IO_LOW ] . empty ( ) & &
( ! queue_ [ Env : : IO_LOW ] . empty ( ) & &
& r = = queue_ [ Env : : IO_LOW ] . front ( ) ) ) ) {
& r = = queue_ [ Env : : IO_LOW ] . front ( ) ) ) ) {
leader_ = & r ;
leader_ = & r ;
timedout = r . cv . TimedWait ( next_refill_us_ ) ;
int64_t delta = next_refill_us_ - NowMicrosMonotonic ( env_ ) ;
delta = delta > 0 ? delta : 0 ;
if ( delta = = 0 ) {
timedout = true ;
} else {
int64_t wait_until = env_ - > NowMicros ( ) + delta ;
timedout = r . cv . TimedWait ( wait_until ) ;
}
} else {
} else {
// Not at the front of queue or an leader has already been elected
// Not at the front of queue or an leader has already been elected
r . cv . Wait ( ) ;
r . cv . Wait ( ) ;
@ -178,7 +185,7 @@ void GenericRateLimiter::Request(int64_t bytes, const Env::IOPriority pri) {
void GenericRateLimiter : : Refill ( ) {
void GenericRateLimiter : : Refill ( ) {
TEST_SYNC_POINT ( " GenericRateLimiter::Refill " ) ;
TEST_SYNC_POINT ( " GenericRateLimiter::Refill " ) ;
next_refill_us_ = env_ - > NowMicros ( ) + refill_period_us_ ;
next_refill_us_ = NowMicrosMonotonic ( env_ ) + refill_period_us_ ;
// Carry over the left over quota from the last period
// Carry over the left over quota from the last period
auto refill_bytes_per_period =
auto refill_bytes_per_period =
refill_bytes_per_period_ . load ( std : : memory_order_relaxed ) ;
refill_bytes_per_period_ . load ( std : : memory_order_relaxed ) ;