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

feat: add support for context.Context in Retry #5

Merged
merged 1 commit into from
Jan 19, 2021

Conversation

smira
Copy link
Member

@smira smira commented Jan 18, 2021

Existing function Retry is maintained with the same API to preserve
backwards compatibility.

New function RetryWithContext accepts a context and delivers it to the
retryable function. Each retry attempt can be limited by timeout (via
context, WithAttemptTimeout).

Garbage-collected channel C which wasn't used.

Replaced stop channel delivery with close(ch) to avoid having a
channel with a single entry.

Removed time.After() as it leaks the object until timer fires.

Fixes #2

Signed-off-by: Andrey Smirnov [email protected]

Existing function `Retry` is maintained with the same API to preserve
backwards compatibility.

New function `RetryWithContext` accepts a context and delivers it to the
retryable function. Each retry attempt can be limited by timeout (via
context, `WithAttemptTimeout`).

Garbage-collected channel `C` which wasn't used.

Replaced stop channel delivery with `close(ch)` to avoid having a
channel with a single entry.

Removed `time.After()` as it leaks the object until timer fires.

Fixes siderolabs#2

Signed-off-by: Andrey Smirnov <[email protected]>
@smira
Copy link
Member Author

smira commented Jan 18, 2021

/approve

@@ -138,7 +146,7 @@ func (t ticker) StopChan() <-chan struct{} {
}

func (t ticker) Stop() {
t.s <- struct{}{}
close(t.s)
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if that should be wrapped with sync.Once to allow several Stop() calls like time.Ticker allows

Copy link
Member Author

Choose a reason for hiding this comment

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

I think sync.Once is pretty heavyweight, I don't think we even ever used .Stop() in our codebase (never saw it myself, but still could be used somewhere).

Probably even .Stop() is no longer required given that we have a context which can be canceled (but that is not backwards compatible).

Copy link
Member Author

Choose a reason for hiding this comment

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

so, I would rather not do it unless we see a big use case for .Stop()

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, makes sense

@smira
Copy link
Member Author

smira commented Jan 19, 2021

/lgtm

@talos-bot talos-bot merged commit b9dc1a9 into siderolabs:master Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add support for context.Context
5 participants