Skip to content

DynamicTokenBucket::consumeWithBorrowNonBlocking will return incorrect value with very low rate #2430

Description

@erin2722

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions