github.com/cilium/cilium@v1.16.2/pkg/endpointmanager/subscribe.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package endpointmanager 5 6 import "github.com/cilium/cilium/pkg/endpoint" 7 8 // Subscribers may register via Subscribe() to be notified when events occur. 9 type Subscriber interface { 10 // EndpointCreated is called at the end of endpoint creation. 11 // Implementations must not attempt write operations on the 12 // EndpointManager from this callback. 13 // This function is being called inside a RLock, so it must not attempt 14 // to acquire a lock on the EndpointManager. 15 EndpointCreated(ep *endpoint.Endpoint) 16 17 // EndpointDeleted is called at the end of endpoint deletion. 18 // Implementations must not attempt write operations on the 19 // EndpointManager from this callback. 20 // This function is being called inside a RLock, so it must not attempt 21 // to acquire a lock on the EndpointManager. 22 EndpointDeleted(ep *endpoint.Endpoint, conf endpoint.DeleteConfig) 23 24 // EndpointRestored is called at the end of endpoint restoration. 25 // Implementations must not attempt write operations on the 26 // EndpointManager from this callback. 27 // This function is being called inside a RLock, so it must not attempt 28 // to acquire a lock on the EndpointManager. 29 EndpointRestored(ep *endpoint.Endpoint) 30 } 31 32 func (mgr *endpointManager) Subscribe(s Subscriber) { 33 mgr.mutex.Lock() 34 defer mgr.mutex.Unlock() 35 36 mgr.subscribers[s] = struct{}{} 37 } 38 39 func (mgr *endpointManager) Unsubscribe(s Subscriber) { 40 mgr.mutex.Lock() 41 defer mgr.mutex.Unlock() 42 delete(mgr.subscribers, s) 43 }