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
23 changes: 19 additions & 4 deletions tower/src/retry/budget/tps_budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Makes sense, done. Anything at or below 0.5 keeps the old (1, 1/percent) pair, and only above that does it scale by 1000, since that's where the reciprocal truncates to 1. Worth noting the small values still truncate (0.3 gives 3, so effectively 33%), but that's the existing behavior and the numbers stay small.

} else if retry_percent <= 0.5 {
(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.
// Above 0.5, `1.0 / retry_percent` truncates to 1 (or 0 past 1.0),
// so scale deposits by 1000 to keep the ratio. For example
// 0.6 -> (1000, 1666) gives 6 retries per 10 deposits, and
// 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 +258,18 @@ 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
);
}
}