Fix misleading zeroTime documentation in TokenBucket.h#2658
Open
UditDewan wants to merge 1 commit into
Open
Conversation
The constructors and reset() of TokenBucketStorage, BasicDynamicTokenBucket, and BasicTokenBucket document the default zeroTime = 0 as producing an empty bucket. The actual balance is min((time - zeroTime) * rate, burstSize), so zeroTime = 0 (the clock epoch) yields a *full* bucket at all but very low rates, and a partially-filled one at very low rates. This mismatch caused the confusion in facebook#2430, where a default-constructed DynamicTokenBucket at rate 0.0001 was expected to hold one token but held only uptime * rate tokens. Document the real semantics and how to start with an actually-empty bucket. Fixes facebook#2430 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The constructors and
reset()ofTokenBucketStorage,BasicDynamicTokenBucket, andBasicTokenBucketdocument the defaultzeroTime = 0as producing an empty bucket. The actual balance ismin((time - zeroTime) * rate, burstSize), so the default of 0 (the clock epoch -- machine boot forsteady_clock) yields a full bucket at all but very low rates, and a partially-filled one at very low rates.This mismatch is the root cause of the confusion in #2430: a default-constructed
DynamicTokenBucketatrate = 0.0001was expected to hold one token (empty buckets that immediately served single-token consumes at normal rates reinforced that expectation), but it actually helduptime * ratetokens (~0.376 on the reporter's machine), soconsumeWithBorrowNonBlockingcorrectly borrowed the remaining ~0.624 tokens and returned a wait of0.624 / 0.0001 = 6241.23s-- exactly the value in the reported failure. There is no precision bug; the documentation was describing semantics the code never had.This updates the five affected doc comments to state the real balance formula and how to start with an actually-empty bucket (pass the current time /
defaultClockNow()aszeroTime).Fixes #2430
Test plan
Documentation-only change; no code modified.
🤖 Generated with Claude Code