-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
retry: Add Budget
trait
#703
retry: Add Budget
trait
#703
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a great start, I left some feedback. I think we should break this into some smaller PRs so the diffs are easier to review and there is less changing.
tower/src/retry/budget.rs
Outdated
@@ -89,21 +89,51 @@ use tokio::time::Instant; | |||
/// For more info about [`Budget`], please see the [module-level documentation]. | |||
/// | |||
/// [module-level documentation]: self | |||
pub struct Budget { | |||
bucket: Bucket, | |||
pub struct Budget<B = TpsBucket> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I would not add a generic here and instead move this whole file to a new submodule and then have the main trait in the mod file.
tower/src/retry/budget.rs
Outdated
#[derive(Debug)] | ||
pub struct Overdrawn { | ||
_inner: (), | ||
pub struct SimpleBucket { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding the new simple bucket here lets start smaller and just do the moving around for now. Then we can add a new impl in a future PR.
tower/src/retry/budget.rs
Outdated
// ===== impl Budget ===== | ||
|
||
impl Budget { | ||
/// Create a [`Budget`] that allows for a certain percent of the total |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
Budget
trait and implement a simple non-time based Budget
Budget
trait
tower/src/retry/budget/mod.rs
Outdated
pub use budget::Budget; | ||
pub use budget::TpsBucket; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these can be combined
tower/src/retry/budget/mod.rs
Outdated
//! } | ||
//! ``` | ||
|
||
#[allow(clippy::module_inception)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huh never seen this lint before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait ya why do we have a mod budget inside the budget module already? shouldn't this mod be in lib.rs only?
tower/src/retry/budget/mod.rs
Outdated
/// For more info about [`Budget`], please see the [module-level documentation]. | ||
/// | ||
/// [module-level documentation]: self | ||
pub trait BudgetTrait { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can just be Budget with the prev type renamed to TpsBudget
. what do you think?
tower/src/retry/budget/mod.rs
Outdated
/// For more info about [`Budget`], please see the [module-level documentation]. | ||
/// | ||
/// [module-level documentation]: self | ||
pub trait Bucket { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure why we need a bucket trait? I imagine we only need the budget trait to abstract these two types we want to have? I don' think we need a second trait to represent internal implementation details? Happy to chat on discord about this if it doesn't make sense what I am saying.
@@ -1,75 +1,4 @@ | |||
//! A retry "budget" for allowing only a certain amount of retries over time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this file not getting highlighted....
tower/src/retry/budget/budget.rs
Outdated
pub struct Budget { | ||
bucket: Bucket, | ||
/// [module-level documentation]: super | ||
pub struct Budget<B = TpsBucket> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think all we need to do to this type is keep it as it was before and move the fn for deposit/withdraw to the trait?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I have like one more request and then I think we are good to merge! Thanks for being patient wtih me!
tower/src/retry/budget/tps_budget.rs
Outdated
pub struct Budget<B = TpsBucket> { | ||
bucket: B, | ||
pub struct TpsBudget { | ||
bucket: TpsBucket, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this extra struct and just merge everything into the TpsBucket and rename that TpsBudget. That should simplify some of this code and also make it less confusing since TpsBucket looks like TpsBudget lol
tower/src/retry/budget/tps_budget.rs
Outdated
@@ -127,7 +125,7 @@ impl Budget { | |||
} | |||
} | |||
|
|||
impl<B: Bucket> BudgetTrait for Budget<B> { | |||
impl Budget for TpsBudget { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to check we should remove the deposit/withdraw methods from the base impl TpsBudget and only have the trait impl ones. I can't see it in the diff but just want to check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I already removed them from the base impl.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This PR adds
BudgetTrait
andBucket
traits to allow users to implement their budget and bucket implementations. The currentBudget
struct is changed to be generic overBucket
. The currentBucket
implementation is also renamed asTpsBucket
.A new
SimpleBucket
struct is added. This bucket works by increasing the cost of the next retry on each retry request. Cost is then decreased gradually on each successful request.