Skip to content

Commit

Permalink
Merge pull request #1345 from charith-elastic/fix/source-dest-lock
Browse files Browse the repository at this point in the history
🐛 Prevent source.Channel from shutting down immediately
  • Loading branch information
k8s-ci-robot committed Jan 19, 2021
2 parents 3c5b358 + 25d539c commit 73c52e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 15 additions & 11 deletions pkg/internal/controller/controller.go
Expand Up @@ -163,9 +163,7 @@ func (c *Controller) Start(ctx context.Context) error {
for _, watch := range c.startWatches {
c.Log.Info("Starting EventSource", "source", watch.src)

watchStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
defer cancel()
if err := watch.src.Start(watchStartCtx, watch.handler, c.Queue, watch.predicates...); err != nil {
if err := watch.src.Start(ctx, watch.handler, c.Queue, watch.predicates...); err != nil {
return err
}
}
Expand All @@ -179,15 +177,21 @@ func (c *Controller) Start(ctx context.Context) error {
continue
}

// use a context with timeout for launching sources and syncing caches.
sourceStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
defer cancel()
if err := func() error {
// use a context with timeout for launching sources and syncing caches.
sourceStartCtx, cancel := context.WithTimeout(ctx, c.CacheSyncTimeout)
defer cancel()

// WaitForSync waits for a definitive timeout, and returns if there
// is an error or a timeout
if err := syncingSource.WaitForSync(sourceStartCtx); err != nil {
err := fmt.Errorf("failed to wait for %s caches to sync: %w", c.Name, err)
c.Log.Error(err, "Could not wait for Cache to sync")
return err
}

// WaitForSync waits for a definitive timeout, and returns if there
// is an error or a timeout
if err := syncingSource.WaitForSync(sourceStartCtx); err != nil {
err := fmt.Errorf("failed to wait for %s caches to sync: %w", c.Name, err)
c.Log.Error(err, "Could not wait for Cache to sync")
return nil
}(); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/source/source.go
Expand Up @@ -215,7 +215,10 @@ func (cs *Channel) Start(
}

dst := make(chan event.GenericEvent, cs.DestBufferSize)

cs.destLock.Lock()
cs.dest = append(cs.dest, dst)
cs.destLock.Unlock()

cs.once.Do(func() {
// Distribute GenericEvents to all EventHandler / Queue pairs Watching this source
Expand All @@ -238,9 +241,6 @@ func (cs *Channel) Start(
}
}()

cs.destLock.Lock()
defer cs.destLock.Unlock()

return nil
}

Expand Down

0 comments on commit 73c52e8

Please sign in to comment.