Skip to content

redisconn: optionally retry the initial PING - #571

Open
Stasky745 wants to merge 1 commit into
stacklok:mainfrom
Stasky745:redisconn-connect-retry
Open

Stasky745 wants to merge 1 commit into
stacklok:mainfrom
Stasky745:redisconn-connect-retry

Conversation

@Stasky745

Copy link
Copy Markdown

Summary

NewClient pings Redis once and returns an error if that fails, so a process that starts while Redis is briefly unavailable exits. In Kubernetes this puts the ToolHive proxy (embedded auth server with Redis storage) into CrashLoopBackOff whenever Redis is rescheduled at the same time as the proxies, for example after a node failure.

This adds an opt-in Config.ConnectRetryTimeout:

  • When set, NewClient retries the initial PING with backoff (100ms, doubling, capped at 2s) until it succeeds, the timeout elapses, or the caller's context is done. The timeout bounds every attempt, including the first, so the total wait never exceeds it, which callers can size against Kubernetes probe budgets.
  • Errors a retry can't fix fail immediately: authentication (WRONGPASS/NOAUTH), permission (NOPERM) and TLS certificate verification errors, so a misconfiguration isn't hidden behind the retry window.
  • On failure it returns the last PING error (for example connection refused) rather than the deadline error, so the cause stays visible. If the caller's context ended, its error is joined in, so errors.Is(err, context.Canceled) still works.
  • The zero value keeps the current single attempt, so existing callers (session storage, vMCP) are unaffected.
  • Negative values are rejected by Validate.

The embedded auth server will opt in, with a default, in a follow-up PR in toolhive.

Refs stacklok/toolhive#7260

Test plan

  • task (lint + tests)
  • scripts/check-redisconn-modules.sh, and go mod tidy/verify, go vet, go test -race for each redisconn module
  • New table-driven TestNewClientConnectRetry:
    • connects once Redis starts late (after go-redis's own dial retries, so the retry loop is what reaches it)
    • gives up after the retry timeout, with the first attempt included in the bound
    • stops at the caller's deadline, and the error matches context.DeadlineExceeded
    • doesn't retry auth errors
    • makes a single attempt with a zero timeout
  • Added a TestConfigValidate case for a negative timeout

Signed-off-by: Roc Ribera <roc@mailerlite.com>
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.

1 participant