folly
folly copied to clipboard
DynamicTokenBucket::consumeWithBorrowNonBlocking will return incorrect value with very low rate
The following unit test demonstrates the issue:
TEST(TokenBucketTest, CanConsumeFirstTokenLowRate) {
folly::DynamicTokenBucket tokenBucket;
ASSERT_EQ(tokenBucket.consumeWithBorrowNonBlocking(1.0 /** toConsume*/, .0001 /** rate*/, 1.0 /** burstSize*/), 0.0);
}
IIUC, this should succeed because the bucket is initialized with 1.0 token, and we are only attempting to consume this first token, and no wait should be required. However, for an excessively low rate (like .0001, this does not repro with .001), the test fails instead:
{"t":{"$date":"2025-05-02T12:53:32.953+00:00"},"s":"I", "c":"TEST", "id":8423378, "ctx":"main","msg":"Test Failed","attr":{"testName":"TokenBucketTest/CanConsumeFirstTokenLowRate","exception":"TestAssertionFailureException","error":"Expected tokenBucket.consumeWithBorrowNonBlocking(1.0, .0001, 1.0) == 0.0 ( 6241.23 == 0) @src/third_party/folly/test/token_bucket_test.cpp:47"}}
There may be a precision bug in the code-- I think it would also be acceptable to document the maximum precision of the rate if such a limit exists.