Skip to content

Commit 434c415

Browse files
committed
Wait for cache sync with timeout before engaging cluster
To prevent opaque errors if a wait for cache sync happens somewhere and silently blocks everything. Signed-off-by: Nelo-T. Wallus <[email protected]> Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent e1dce12 commit 434c415

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

pkg/clusters/clusters.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"maps"
2424
"slices"
2525
"sync"
26+
"time"
2627

2728
"github.com/google/go-cmp/cmp"
2829

@@ -45,6 +46,11 @@ type Clusters[T cluster.Cluster] struct {
4546
// adding or replacing clusters.
4647
EqualClusters func(a, b T) bool
4748

49+
// WaitCacheTimeout is the duration to wait for a cluster's cache
50+
// to sync when adding a new cluster.
51+
// Default is 30 seconds.
52+
WaitCacheTimeout time.Duration
53+
4854
lock sync.RWMutex
4955
clusters map[string]T
5056
cancels map[string]context.CancelFunc
@@ -63,10 +69,11 @@ type Index struct {
6369
// New returns a new instance of Clusters.
6470
func New[T cluster.Cluster]() Clusters[T] {
6571
return Clusters[T]{
66-
EqualClusters: EqualClusters[T],
67-
clusters: make(map[string]T),
68-
cancels: make(map[string]context.CancelFunc),
69-
indexers: []Index{},
72+
EqualClusters: EqualClusters[T],
73+
WaitCacheTimeout: 30 * time.Second,
74+
clusters: make(map[string]T),
75+
cancels: make(map[string]context.CancelFunc),
76+
indexers: []Index{},
7077
}
7178
}
7279

@@ -113,6 +120,14 @@ func (c *Clusters[T]) Add(ctx context.Context, clusterName string, cl T, aware m
113120
}
114121
}()
115122

123+
waitCacheCtx, cancel := context.WithTimeout(ctx, c.WaitCacheTimeout)
124+
defer cancel()
125+
126+
if !cl.GetCache().WaitForCacheSync(waitCacheCtx) {
127+
defer c.Remove(clusterName)
128+
return fmt.Errorf("timed out after %q waiting for cache to sync for cluster %s", c.WaitCacheTimeout, clusterName)
129+
}
130+
116131
if aware != nil {
117132
if err := aware.Engage(ctx, clusterName, cl); err != nil {
118133
defer c.Remove(clusterName)

0 commit comments

Comments
 (0)