Skip to content

fix(retry): avoid truncating fractional retry_percent in TpsBudget#863

Open
SAY-5 wants to merge 2 commits into
tower-rs:masterfrom
SAY-5:fix-tps-budget-fractional
Open

fix(retry): avoid truncating fractional retry_percent in TpsBudget#863
SAY-5 wants to merge 2 commits into
tower-rs:masterfrom
SAY-5:fix-tps-budget-fractional

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented May 12, 2026

TpsBudget::new used (1, (1.0 / retry_percent) as isize) for retry_percent <= 1.0, which truncates the reciprocal so e.g. 0.6 authorizes a full retry per deposit instead of 0.6. This reuses the existing 1000x deposit scaling for all positive percentages and adds a regression test. Closes #857.

// If there is no percent, then you gain nothing from deposits.
// Withdrawals can only be made against the reserve, over time.
(0, 1)
} else if retry_percent <= 1.0 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think most often the percent will be 10, 20, around there. So, to not need to deal with large numbers in the most common case, this could probably be edit to just <= 0.5. What do you think?

@SAY-5 SAY-5 force-pushed the fix-tps-budget-fractional branch from 56de74d to d769498 Compare May 12, 2026 23:38
@SAY-5
Copy link
Copy Markdown
Author

SAY-5 commented May 12, 2026

Good call, switched to scaling only when retry_percent <= 0.5 so the common integer-ish percents keep the original arithmetic. Updated the test name to match.

Collapses the fractional-percent and > 1 branches into one: scale
deposits by 1000 and use (1000 / retry_percent) as the withdraw cost
for every non-zero percent. This gives exact precision across the
full [0, 1000] range without a special-case threshold.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@SAY-5
Copy link
Copy Markdown
Author

SAY-5 commented May 27, 2026

Updated: applied the 1000x scaling uniformly for all non-zero retry_percent values (not just <= 0.5). This means the test case at 0.6 now actually verifies the fix: (1000, 1666) gives exactly 6 retries per 10 deposits. The > 1 case also simplifies to the same formula.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

retry: TpsBudget over-allocates retries for fractional retry_percent < 1.0

2 participants