Skip to content

Commit

Permalink
policy: lazily start SelectorCache.handleUserNotifications
Browse files Browse the repository at this point in the history
Hive wants components to not start any goroutines during startup.
Change SelectorCache so that handleUserNotifications is started
when the first notification is queued, not when SelectorCache is
created.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb authored and christarazi committed Mar 13, 2023
1 parent 7087445 commit f5d6080
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 0 additions & 1 deletion daemon/cmd/cells_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var goleakOptions = []goleak.Option{
// Ignore goroutines started by the policy trifecta, see [newPolicyTrifecta].
goleak.IgnoreTopFunction("github.com/cilium/cilium/pkg/identity/cache.(*identityWatcher).watch.func1"),
goleak.IgnoreTopFunction("github.com/cilium/cilium/pkg/trigger.(*Trigger).waiter"),
goleak.IgnoreTopFunction("sync.runtime_notifyListWait"),
}

// TestAgentCell verifies that the Agent hive can be instantiated with
Expand Down
8 changes: 6 additions & 2 deletions pkg/policy/selectorcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ type SelectorCache struct {
userMutex lock.Mutex
// userNotes holds a FIFO list of user notifications to be made
userNotes []userNotification

// used to lazily start the handler for user notifications.
startNotificationsHandlerOnce sync.Once
}

// GetModel returns the API model of the SelectorCache.
Expand Down Expand Up @@ -267,6 +270,9 @@ func (sc *SelectorCache) handleUserNotifications() {
}

func (sc *SelectorCache) queueUserNotification(user CachedSelectionUser, selector CachedSelector, added, deleted []identity.NumericIdentity, wg *sync.WaitGroup) {
sc.startNotificationsHandlerOnce.Do(func() {
go sc.handleUserNotifications()
})
wg.Add(1)
sc.userMutex.Lock()
sc.userNotes = append(sc.userNotes, userNotification{
Expand All @@ -288,8 +294,6 @@ func NewSelectorCache(allocator cache.IdentityAllocator, ids cache.IdentityCache
selectors: make(map[string]identitySelector),
}
sc.userCond = sync.NewCond(&sc.userMutex)
go sc.handleUserNotifications()

return sc
}

Expand Down

0 comments on commit f5d6080

Please sign in to comment.