Skip to content

Commit f4ac1b2

Browse files
authored
fix: Retry connection to valkey (#1700)
Fixes #1698 NewClient tries to dial the valkey instance before returning. If there's a problem with dialing valkey, it will fail and not recover. As a result, the cloud run instance will fail to deploy. This change adds retry logic in an attempt to give the server more attempts before failing.
1 parent cee9395 commit f4ac1b2

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
firebase.google.com/go/v4 v4.18.0
1010
github.com/GoogleChrome/webstatus.dev/lib/gen v0.0.0-20250804165824-fd606c7d439d
1111
github.com/antlr4-go/antlr/v4 v4.13.1
12+
github.com/cenkalti/backoff/v5 v5.0.3
1213
github.com/deckarep/golang-set v1.8.0
1314
github.com/google/go-github/v73 v73.0.0
1415
github.com/oapi-codegen/runtime v1.1.2
@@ -32,7 +33,6 @@ require (
3233
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.53.0 // indirect
3334
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.53.0 // indirect
3435
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
35-
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
3636
github.com/containerd/errdefs v1.0.0 // indirect
3737
github.com/containerd/errdefs/pkg v0.3.0 // indirect
3838
github.com/containerd/platforms v0.2.1 // indirect

lib/valkeycache/cache.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/GoogleChrome/webstatus.dev/lib/cachetypes"
24+
"github.com/cenkalti/backoff/v5"
2425
"github.com/valkey-io/valkey-go"
2526
)
2627

@@ -42,10 +43,19 @@ func NewValkeyDataCache[K comparable, V []byte](
4243
ttl time.Duration) (*ValkeyDataCache[K, V], error) {
4344

4445
addr := fmt.Sprintf("%s:%s", host, port)
45-
// nolint: exhaustruct // No need to use every option of 3rd party struct.
46-
c, err := valkey.NewClient(valkey.ClientOption{
47-
InitAddress: []string{addr},
48-
})
46+
operation := func() (valkey.Client, error) {
47+
// nolint: exhaustruct // No need to use every option of 3rd party struct.
48+
return valkey.NewClient(valkey.ClientOption{
49+
InitAddress: []string{addr},
50+
})
51+
}
52+
53+
c, err := backoff.Retry(context.TODO(), operation,
54+
backoff.WithBackOff(backoff.NewExponentialBackOff()),
55+
// Should be less than the total time in the startup probe for the backend container in
56+
// infra/backend/service.tf
57+
backoff.WithMaxElapsedTime(25*time.Second),
58+
)
4959
if err != nil {
5060
return nil, err
5161
}

0 commit comments

Comments
 (0)