-
Notifications
You must be signed in to change notification settings - Fork 49
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
feat: Added Reset to TokenBucket #90
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,33 @@ func (t *TokenBucket) Limit(ctx context.Context) (time.Duration, error) { | |
return t.Take(ctx, 1) | ||
} | ||
|
||
// Reset the bucket to zero state | ||
func (t *TokenBucket) Reset(ctx context.Context) (time.Duration, error) { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
if err := t.locker.Lock(ctx); err != nil { | ||
return 0, err | ||
} | ||
defer func() { | ||
if err := t.locker.Unlock(ctx); err != nil { | ||
t.logger.Log(err) | ||
} | ||
}() | ||
state, err := t.backend.State(ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you don't need you retrieve the state first and immediately override its fields. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohh yeah, my bad i will remove the code. Is the other stuff fine? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Race condition occurs while using a new TokenBucketState. Without retrieving the state and using it, error occurs. For Reference Memcache CompareAndSwap. It expects the item's key to be the same while other values may differ. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can skip that check if the state is zero.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively we may consider adding a separate What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like this? #91 |
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
// Reset bucket to initial state | ||
state.Available = 0 | ||
state.Last = 0 | ||
|
||
if err = t.backend.SetState(ctx, state); err != nil { | ||
return 0, err | ||
} | ||
return 0, nil | ||
} | ||
|
||
// TokenBucketInMemory is an in-memory implementation of TokenBucketStateBackend. | ||
// | ||
// The state is not shared nor persisted so it won't survive restarts or failures. | ||
|
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.
What is the returned
time.Duration
value used for?Is it needed? If so, please add a comment explaining what it means.