Skip to content

feat: retry connection on startup#7

Open
julio4 wants to merge 1 commit into
mainfrom
feat/rpc-retry
Open

feat: retry connection on startup#7
julio4 wants to merge 1 commit into
mainfrom
feat/rpc-retry

Conversation

@julio4
Copy link
Copy Markdown
Member

@julio4 julio4 commented Mar 10, 2026

No description provided.

Comment thread rpc/rpc.go
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)
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion:

pls move it into helpers.go (exactly the file for this sort of helper function)

Comment thread rpc/rpc.go

// 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) {
Copy link
Copy Markdown
Member

@0x416e746f6e 0x416e746f6e Apr 3, 2026

Choose a reason for hiding this comment

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

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

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.

2 participants