|
| 1 | +package async |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "sync/atomic" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// Event types for routine lifecycle and snapshotting |
| 11 | +type routineLifeCycleEventType = int |
| 12 | +type routineSnapshottingEventType = int |
| 13 | + |
| 14 | +const ( |
| 15 | + routineStarted routineLifeCycleEventType = iota |
| 16 | + routineEnded |
| 17 | + routineTimeboxExceeded |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + routineCount routineSnapshottingEventType = iota |
| 22 | + routineByNameCount |
| 23 | +) |
| 24 | + |
| 25 | +// DefaultAsyncObserverBufferSize defines the default buffer size for the event channel |
| 26 | +// used by async routine observers. This value determines how many events can be queued |
| 27 | +// before send operations block or, if not guaranteed, events are dropped. |
| 28 | +const DefaultAsyncObserverBufferSize = 16 |
| 29 | + |
| 30 | +type asyncRoutineObserverOption func(o *asyncRoutineObserver) |
| 31 | + |
| 32 | +// WithObserverTimeout sets a timeout for the observer routine. |
| 33 | +func WithObserverTimeout(timeout time.Duration) func(o *asyncRoutineObserver) { |
| 34 | + return func(o *asyncRoutineObserver) { |
| 35 | + o.timeout = &timeout |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// WithObserverRoutineData adds custom data to the observer routine. |
| 40 | +func WithObserverRoutineData(key string, value string) func(o *asyncRoutineObserver) { |
| 41 | + return func(o *asyncRoutineObserver) { |
| 42 | + if o.observerNotifierRoutineData == nil { |
| 43 | + o.observerNotifierRoutineData = make(routineData) |
| 44 | + } |
| 45 | + o.observerNotifierRoutineData[key] = value |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// WithChannelSize sets the buffer size for the async observer's event channel. |
| 50 | +// Use this as an option when creating the async observer. |
| 51 | +func WithChannelSize(size int) func(o *asyncRoutineObserver) { |
| 52 | + return func(o *asyncRoutineObserver) { |
| 53 | + if o.channel != nil { |
| 54 | + close(o.channel) |
| 55 | + } |
| 56 | + o.channel = make(chan asyncRoutineEvent, size) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// WithGuaranteedDelivery enables guaranteed delivery mode for the async observer's events. |
| 61 | +// When enabled, the observer will not drop events if the channel is full, but will block until space is available. |
| 62 | +// Use this as an option when creating the observer. |
| 63 | +func WithGuaranteedDelivery() func(o *asyncRoutineObserver) { |
| 64 | + return func(o *asyncRoutineObserver) { |
| 65 | + o.guaranteedDelivery = true |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func NewAsyncRoutineObserver(delegate RoutinesObserver, options ...asyncRoutineObserverOption) RoutinesObserver { |
| 70 | + asyncObserver := asyncRoutineObserver{ |
| 71 | + delegate: delegate, |
| 72 | + guaranteedDelivery: false, |
| 73 | + } |
| 74 | + |
| 75 | + asyncObserver.channelIsOpen.Store(true) |
| 76 | + |
| 77 | + for _, option := range options { |
| 78 | + option(&asyncObserver) |
| 79 | + } |
| 80 | + |
| 81 | + if asyncObserver.channel == nil { |
| 82 | + asyncObserver.channel = make(chan asyncRoutineEvent, DefaultAsyncObserverBufferSize) |
| 83 | + } |
| 84 | + |
| 85 | + asyncObserver.startObserving() |
| 86 | + return &asyncObserver |
| 87 | +} |
| 88 | + |
| 89 | +var _ RoutinesObserver = (*asyncRoutineObserver)(nil) |
| 90 | + |
| 91 | +type asyncRoutineObserver struct { |
| 92 | + delegate RoutinesObserver |
| 93 | + channel chan asyncRoutineEvent |
| 94 | + channelIsOpen atomic.Bool |
| 95 | + timeout *time.Duration |
| 96 | + observerNotifierRoutineData routineData |
| 97 | + closeOnce sync.Once |
| 98 | + guaranteedDelivery bool |
| 99 | +} |
| 100 | + |
| 101 | +func (a *asyncRoutineObserver) RoutineStarted(routine AsyncRoutine) { |
| 102 | + a.notify(&routineLifecycleEvent{ |
| 103 | + evtType: routineStarted, |
| 104 | + routine: routine, |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +func (a *asyncRoutineObserver) RoutineFinished(routine AsyncRoutine) { |
| 109 | + a.notify(&routineLifecycleEvent{ |
| 110 | + evtType: routineEnded, |
| 111 | + routine: routine, |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func (a *asyncRoutineObserver) RoutineExceededTimebox(routine AsyncRoutine) { |
| 116 | + a.notify(&routineLifecycleEvent{ |
| 117 | + evtType: routineTimeboxExceeded, |
| 118 | + routine: routine, |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func (a *asyncRoutineObserver) RunningRoutineCount(count int) { |
| 123 | + a.notify(&routineCountEvent{ |
| 124 | + count: count, |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +func (a *asyncRoutineObserver) RunningRoutineByNameCount(name string, count int) { |
| 129 | + a.notify(&routineByNameCountEvent{ |
| 130 | + routineName: name, |
| 131 | + count: count, |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +func (a *asyncRoutineObserver) manageLifecycleEvent(evt *routineLifecycleEvent) { |
| 136 | + switch evt.evtType { |
| 137 | + case routineStarted: |
| 138 | + a.delegate.RoutineStarted(evt.routine) |
| 139 | + case routineEnded: |
| 140 | + a.delegate.RoutineFinished(evt.routine) |
| 141 | + case routineTimeboxExceeded: |
| 142 | + a.delegate.RoutineExceededTimebox(evt.routine) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func (a *asyncRoutineObserver) startObserving() { |
| 147 | + routine := NewAsyncRoutine("async-observer-notifier", context.Background(), func() { |
| 148 | + for evt := range a.channel { |
| 149 | + switch evt.(type) { |
| 150 | + case *routineLifecycleEvent: |
| 151 | + a.manageLifecycleEvent(evt.(*routineLifecycleEvent)) |
| 152 | + case *routineCountEvent: |
| 153 | + a.delegate.RunningRoutineCount(evt.(*routineCountEvent).count) |
| 154 | + case *routineByNameCountEvent: |
| 155 | + evt := evt.(*routineByNameCountEvent) |
| 156 | + a.delegate.RunningRoutineByNameCount(evt.routineName, evt.count) |
| 157 | + } |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | + for key, value := range a.observerNotifierRoutineData { |
| 162 | + routine = routine.WithData(key, value) |
| 163 | + } |
| 164 | + |
| 165 | + if a.timeout != nil { |
| 166 | + routine = routine.Timebox(*a.timeout) |
| 167 | + } |
| 168 | + |
| 169 | + routine.Run() |
| 170 | +} |
| 171 | + |
| 172 | +func (a *asyncRoutineObserver) stopObserving() { |
| 173 | + a.closeOnce.Do(func() { |
| 174 | + a.channelIsOpen.Store(false) |
| 175 | + close(a.channel) |
| 176 | + }) |
| 177 | +} |
| 178 | + |
| 179 | +// notify sends an event to the observer channel, non-blocking if closed. |
| 180 | +func (a *asyncRoutineObserver) notify(evt asyncRoutineEvent) { |
| 181 | + if a.guaranteedDelivery { |
| 182 | + if a.channelIsOpen.Load() { |
| 183 | + a.channel <- evt |
| 184 | + } |
| 185 | + // channel is closed. Event is lost. This happens when `stopObserving` is called. |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + select { |
| 190 | + case a.channel <- evt: |
| 191 | + default: |
| 192 | + // Event dropped |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +// --- Event types and interfaces --- |
| 197 | + |
| 198 | +type asyncRoutineEvent interface { |
| 199 | + getEventType() int |
| 200 | +} |
| 201 | + |
| 202 | +var _ asyncRoutineEvent = (*routineLifecycleEvent)(nil) |
| 203 | +var _ asyncRoutineEvent = (*routineCountEvent)(nil) |
| 204 | +var _ asyncRoutineEvent = (*routineByNameCountEvent)(nil) |
| 205 | + |
| 206 | +type routineLifecycleEvent struct { |
| 207 | + evtType routineLifeCycleEventType |
| 208 | + routine AsyncRoutine |
| 209 | +} |
| 210 | + |
| 211 | +func (r *routineLifecycleEvent) getEventType() int { |
| 212 | + return r.evtType |
| 213 | +} |
| 214 | + |
| 215 | +type routineCountEvent struct { |
| 216 | + count int |
| 217 | +} |
| 218 | + |
| 219 | +func (r routineCountEvent) getEventType() int { |
| 220 | + return routineCount |
| 221 | +} |
| 222 | + |
| 223 | +type routineByNameCountEvent struct { |
| 224 | + routineName string |
| 225 | + count int |
| 226 | +} |
| 227 | + |
| 228 | +func (r routineByNameCountEvent) getEventType() int { |
| 229 | + return routineByNameCount |
| 230 | +} |
0 commit comments