@@ -127,6 +127,11 @@ type Controller[request comparable] struct {
127
127
// startWatches maintains a list of sources, handlers, and predicates to start when the controller is started.
128
128
startWatches []source.TypedSource [request ]
129
129
130
+ // startedEventSources is used to track if the event sources have been started.
131
+ // It ensures that we append sources to c.startWatches only until we call Start() / Warmup()
132
+ // It is true if startEventSourcesLocked has been called at least once.
133
+ startedEventSources bool
134
+
130
135
// didStartEventSourcesOnce is used to ensure that the event sources are only started once.
131
136
didStartEventSourcesOnce sync.Once
132
137
@@ -199,10 +204,9 @@ func (c *Controller[request]) Watch(src source.TypedSource[request]) error {
199
204
c .mu .Lock ()
200
205
defer c .mu .Unlock ()
201
206
202
- // Controller hasn't started yet, store the watches locally and return.
203
- //
204
- // These watches are going to be held on the controller struct until the manager or user calls Start(...).
205
- if ! c .Started {
207
+ // Sources weren't started yet, store the watches locally and return.
208
+ // These sources are going to be held until either Warmup() or Start(...) is called.
209
+ if ! c .startedEventSources {
206
210
c .startWatches = append (c .startWatches , src )
207
211
return nil
208
212
}
@@ -228,6 +232,9 @@ func (c *Controller[request]) Warmup(ctx context.Context) error {
228
232
c .mu .Lock ()
229
233
defer c .mu .Unlock ()
230
234
235
+ // Set the ctx so later calls to watch use this internal context of nil
236
+ c .ctx = ctx
237
+
231
238
return c .startEventSourcesLocked (ctx )
232
239
}
233
240
@@ -364,6 +371,10 @@ func (c *Controller[request]) startEventSourcesLocked(ctx context.Context) error
364
371
// We should never hold watches more than necessary, each watch source can hold a backing cache,
365
372
// which won't be garbage collected if we hold a reference to it.
366
373
c .startWatches = nil
374
+
375
+ // Mark event sources as started after resetting the startWatches slice to no-op a Watch()
376
+ // call after event sources have been started.
377
+ c .startedEventSources = true
367
378
})
368
379
369
380
return retErr
0 commit comments