Starts the listener queue for the event manager
(ctx context.Context, maxProcesses uint, waitTime int)
| 65 | |
| 66 | // Starts the listener queue for the event manager |
| 67 | func (em *EventManager) Start(ctx context.Context, maxProcesses uint, waitTime int) { |
| 68 | |
| 69 | if maxProcesses == 0 { |
| 70 | maxProcesses = kMaxActiveEvents |
| 71 | } |
| 72 | if waitTime < 0 { |
| 73 | em.waitTime = kEventWaitTime |
| 74 | } else { |
| 75 | em.waitTime = waitTime |
| 76 | } |
| 77 | |
| 78 | base.InfofCtx(ctx, base.KeyEvents, "Starting event manager with max processes:%d, wait time:%d ms", maxProcesses, em.waitTime) |
| 79 | // activeCountChannel limits the number of concurrent events being processed |
| 80 | em.activeCountChannel = make(chan bool, maxProcesses) |
| 81 | |
| 82 | // asyncEventChannel stores the incoming events. It's set to 3x activeCountChannel, to |
| 83 | // handle temporary spikes in event inflow |
| 84 | em.asyncEventChannel = make(chan Event, 3*maxProcesses) |
| 85 | |
| 86 | // Start the event channel worker go routine, which will work the event queue and spawn goroutines to process the |
| 87 | // event. Blocks if the activeCountChannel is full, to prevent spawning more than cap(activeCountChannel) |
| 88 | // goroutines. |
| 89 | go func() { |
| 90 | for { |
| 91 | select { |
| 92 | case <-em.terminator: |
| 93 | return |
| 94 | case event := <-em.asyncEventChannel: |
| 95 | em.activeCountChannel <- true |
| 96 | go em.ProcessEvent(ctx, event) |
| 97 | } |
| 98 | } |
| 99 | }() |
| 100 | |
| 101 | } |
| 102 | |
| 103 | // Concurrent processing of all async event handlers registered for the event type |
| 104 | func (em *EventManager) ProcessEvent(ctx context.Context, event Event) { |