feat: retry connection on startup#7
Open
julio4 wants to merge 1 commit into
Open
Conversation
0x416e746f6e
reviewed
Apr 3, 2026
Comment on lines
+22
to
+46
| const ( | ||
| startupRetryTimeout = 30 * time.Second | ||
| startupRetryInterval = time.Second | ||
| ) | ||
|
|
||
| // RetryOnStartup retries fn until it succeeds or the startup timeout (30s) elapses. | ||
| // Used during initialization to wait for RPC endpoints to become available. | ||
| func RetryOnStartup[T any](fn func() (T, error)) (T, error) { | ||
| deadline := time.Now().Add(startupRetryTimeout) | ||
| for { | ||
| result, err := fn() | ||
| if err == nil { | ||
| return result, nil | ||
| } | ||
| if time.Now().After(deadline) { | ||
| return result, err | ||
| } | ||
| zap.L().Warn("Not ready, retrying...", | ||
| zap.Duration("retry_in", startupRetryInterval), | ||
| zap.Error(err), | ||
| ) | ||
| time.Sleep(startupRetryInterval) | ||
| } | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
suggestion:
pls move it into helpers.go (exactly the file for this sort of helper function)
|
|
||
| // RetryOnStartup retries fn until it succeeds or the startup timeout (30s) elapses. | ||
| // Used during initialization to wait for RPC endpoints to become available. | ||
| func RetryOnStartup[T any](fn func() (T, error)) (T, error) { |
Member
There was a problem hiding this comment.
suggestion:
the name of the function is a bit misleading.
what it does is just keep retrying if there is a failure, which doesn't necessarily happen on startup. it's just because the use of this function is placed around the calls that occur on start, that the "startup" is involved.
better name could be RetryOnFailureFor30s (since 30s is hardcoded)
or RetryOnFailure if you parameterise the timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.