@@ -16,7 +16,7 @@ type AsyncRoutineManager interface {
1616 RemoveObserver (observerId string )
1717 IsEnabled () bool
1818 GetSnapshot () Snapshot
19- notify ( eventSource func ( observer RoutinesObserver ) )
19+ notifyAll ( src AsyncRoutine , evt routineEvent )
2020 Monitor () AsyncRoutineMonitor
2121 register (routine AsyncRoutine )
2222 deregister (routine AsyncRoutine )
@@ -30,7 +30,7 @@ type asyncRoutineManager struct {
3030 snapshottingToggle Toggle
3131 snapshottingInterval time.Duration
3232 routines cmap.ConcurrentMap [string , AsyncRoutine ]
33- observers cmap.ConcurrentMap [string , RoutinesObserver ]
33+ observers cmap.ConcurrentMap [string , * observerProxy ]
3434
3535 monitorLock sync.Mutex // user to sync the `Start` and `Stop` methods that are used to start the
3636 // snapshotting routine
@@ -46,13 +46,27 @@ func (arm *asyncRoutineManager) IsEnabled() bool {
4646// AddObserver adds a new RoutineObserver to the list of observers.
4747// Assigns and returns an observer ID to the RoutineObserver
4848func (arm * asyncRoutineManager ) AddObserver (observer RoutinesObserver ) string {
49+ return arm .AddObserverWithTimeout (observer , DefaultObserverTimeout )
50+ }
51+
52+ // AddObserverWithTimeout registers a new RoutinesObserver with the asyncRoutineManager,
53+ // associating it with a unique identifier and a specified timeout duration.
54+ // The function returns the unique ID assigned to the observer.
55+ func (arm * asyncRoutineManager ) AddObserverWithTimeout (observer RoutinesObserver , timeout time.Duration ) string {
4956 uid := uuid .New ().String ()
50- arm .observers .Set (uid , observer )
57+ proxy := newObserverProxy (uid , observer , arm , timeout )
58+ arm .observers .Set (uid , proxy )
59+ proxy .startObserving ()
5160 return uid
5261}
5362
5463// RemoveObserver removes the given RoutineObserver from the list of observers
5564func (arm * asyncRoutineManager ) RemoveObserver (observerId string ) {
65+ observer , ok := arm .observers .Get (observerId )
66+ if ! ok {
67+ return
68+ }
69+ observer .stopObserving ()
5670 arm .observers .Remove (observerId )
5771}
5872
@@ -64,9 +78,10 @@ func (arm *asyncRoutineManager) GetSnapshot() Snapshot {
6478 return snapshot
6579}
6680
67- func (arm * asyncRoutineManager ) notify (eventSource func (observer RoutinesObserver )) {
81+ // notifyAll notifies all the observers of the event evt received from the routine src
82+ func (arm * asyncRoutineManager ) notifyAll (src AsyncRoutine , evt routineEvent ) {
6883 for _ , observer := range arm .observers .Items () {
69- eventSource ( observer )
84+ observer . notify ( src , evt )
7085 }
7186}
7287
@@ -113,7 +128,7 @@ var lock sync.RWMutex
113128func newAsyncRoutineManager (options ... AsyncManagerOption ) AsyncRoutineManager {
114129 mgr := & asyncRoutineManager {
115130 routines : cmap .New [AsyncRoutine ](),
116- observers : cmap .New [RoutinesObserver ](),
131+ observers : cmap .New [* observerProxy ](),
117132 snapshottingInterval : DefaultRoutineSnapshottingInterval ,
118133 ctx : context .Background (),
119134 managerToggle : func () bool { return true }, // manager is enabled by default
0 commit comments