Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions tower/src/retry/budget/tps_budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ impl TpsBudget {
// 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?

(1, (1.0 / retry_percent) as isize)
} else {
// Support for when retry_percent is between 1.0 and 1000.0,
// meaning for every deposit D, D * retry_percent withdrawals
// can be made.
// Scale deposits by 1000 so fractional percentages (where
// 1/retry_percent truncates badly) and values > 1 both stay
// precise. For example 0.6 -> (1000, 1666) gives exactly
// 6 retries per 10 deposits; 2.0 -> (1000, 500) gives 20.
(1000, (1000.0 / retry_percent) as isize)
};
let reserve = (min_per_sec as isize)
Expand Down Expand Up @@ -257,4 +256,17 @@ mod tests {

assert!(!bgt.withdraw());
}

#[test]
fn tps_fractional_retry_percent_below_one() {
let bgt = TpsBudget::new(Duration::from_secs(1), 0, 0.6);
for _ in 0..10 {
bgt.deposit();
}
let allowed = (0..10).filter(|_| bgt.withdraw()).count();
assert!(
allowed <= 6,
"10 deposits at retry_percent=0.6 should allow at most 6 retries, got {allowed}"
);
}
}