Skip to content

Commit 0a4e4b1

Browse files
authored
OCPBUGS-29479: Add retries to async cache initialization (#13610)
* OCPBUGS-29479: Add retries to async cache initialization Auth initialization can fail if the API server not ready yet. This is especially common during cluster install. * Don't log errors on async cache retries. * Use context.WithTimeout and wait.UntilWithContext instead of a for loop to retry auth async cache setup * Use `wait.PollUntilContextTimeout` for async cache initialization * Address review comments - Log error on each retry - Do not return error from PollUntilContextTimeout condition func - Reduce retry interval to 10 seconds to match historical behavior
1 parent 97b74c6 commit 0a4e4b1

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

pkg/auth/asynccache.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
"k8s.io/klog"
1111
)
1212

13+
const (
14+
initializationRetryInterval = 10 * time.Second
15+
initializationTimeout = 5 * time.Minute
16+
initializeImmediately = true
17+
)
18+
1319
type cachingFuncType[T any] func(ctx context.Context) (T, error)
1420

1521
type AsyncCache[T any] struct {
@@ -27,13 +33,26 @@ func NewAsyncCache[T any](ctx context.Context, reloadPeriod time.Duration, cachi
2733
cachingFunc: cachingFunc,
2834
}
2935

30-
item, err := cachingFunc(ctx)
36+
var err error
37+
wait.PollUntilContextTimeout(
38+
ctx,
39+
initializationRetryInterval,
40+
initializationTimeout,
41+
initializeImmediately,
42+
func(ctx context.Context) (bool, error) {
43+
item, err := cachingFunc(ctx)
44+
if err != nil {
45+
klog.V(4).Infof("failed to setup an async cache (retrying in %v) - caching func returned error: %v", initializationRetryInterval, err)
46+
return false, nil
47+
}
48+
c.cachedItem = item
49+
return true, nil
50+
},
51+
)
52+
3153
if err != nil {
3254
return nil, fmt.Errorf("failed to setup an async cache - caching func returned error: %w", err)
3355
}
34-
35-
c.cachedItem = item
36-
3756
return c, nil
3857
}
3958

0 commit comments

Comments
 (0)