Skip to content
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

Merged
merged 7 commits into from
Oct 24, 2022

Conversation

boraarslan
Copy link
Contributor

This PR adds BudgetTrait and Bucket traits to allow users to implement their budget and bucket implementations. The current Budget struct is changed to be generic over Bucket. The current Bucket implementation is also renamed as TpsBucket.

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.

Copy link
Member

@LucioFranco LucioFranco left a 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.

@@ -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> {
Copy link
Member

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.

#[derive(Debug)]
pub struct Overdrawn {
_inner: (),
pub struct SimpleBucket {
Copy link
Member

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.

// ===== impl Budget =====

impl Budget {
/// Create a [`Budget`] that allows for a certain percent of the total
Copy link
Member

Choose a reason for hiding this comment

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

nice!

@boraarslan boraarslan changed the title retry: Add Budget trait and implement a simple non-time based Budget retry: Add Budget trait Oct 19, 2022
Comment on lines 77 to 78
pub use budget::Budget;
pub use budget::TpsBucket;
Copy link
Member

Choose a reason for hiding this comment

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

these can be combined

//! }
//! ```

#[allow(clippy::module_inception)]
Copy link
Member

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?

Copy link
Member

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?

/// For more info about [`Budget`], please see the [module-level documentation].
///
/// [module-level documentation]: self
pub trait BudgetTrait {
Copy link
Member

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?

/// For more info about [`Budget`], please see the [module-level documentation].
///
/// [module-level documentation]: self
pub trait Bucket {
Copy link
Member

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.
Copy link
Member

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....

pub struct Budget {
bucket: Bucket,
/// [module-level documentation]: super
pub struct Budget<B = TpsBucket> {
Copy link
Member

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?

Copy link
Member

@LucioFranco LucioFranco left a 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!

pub struct Budget<B = TpsBucket> {
bucket: B,
pub struct TpsBudget {
bucket: TpsBucket,
Copy link
Member

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

@@ -127,7 +125,7 @@ impl Budget {
}
}

impl<B: Bucket> BudgetTrait for Budget<B> {
impl Budget for TpsBudget {
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

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

LGTM!

@LucioFranco LucioFranco enabled auto-merge (squash) October 24, 2022 18:24
@LucioFranco LucioFranco merged commit d27ba65 into tower-rs:master Oct 24, 2022
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.

None yet

2 participants