Enqueue pushes the given event onto the EventQueue. If the queue has been stopped, the Event will not be enqueued, and its cancel channel will be closed, indicating that the Event was not ran. This function may block if the queue is at its capacity for events. If a single Event has Enqueue called on
(ev *Event)
| 96 | // if the Event has been previously enqueued, if the Event is nil, or the queue |
| 97 | // itself is not initialized properly. |
| 98 | func (q *EventQueue) Enqueue(ev *Event) (<-chan any, error) { |
| 99 | if q.notSafeToAccess() || ev == nil { |
| 100 | return nil, fmt.Errorf("unable to Enqueue event") |
| 101 | } |
| 102 | |
| 103 | // Events can only be enqueued once. |
| 104 | if !ev.enqueued.CompareAndSwap(false, true) { |
| 105 | return nil, fmt.Errorf("unable to Enqueue event; event has already had Enqueue called on it") |
| 106 | } |
| 107 | |
| 108 | // Multiple Enqueues can occur at the same time. Ensure that events channel |
| 109 | // is not closed while we are enqueueing events. |
| 110 | q.eventsMu.RLock() |
| 111 | defer q.eventsMu.RUnlock() |
| 112 | |
| 113 | select { |
| 114 | // The event should be drained from the queue (e.g., it should not be |
| 115 | // processed). |
| 116 | case <-q.drain: |
| 117 | // Closed eventResults channel signifies cancellation. |
| 118 | close(ev.cancelled) |
| 119 | close(ev.eventResults) |
| 120 | |
| 121 | return ev.eventResults, nil |
| 122 | default: |
| 123 | // The events channel may be closed even if an event has been pushed |
| 124 | // onto the events channel, as events are consumed off of the events |
| 125 | // channel asynchronously! If the EventQueue is closed before this |
| 126 | // event is processed, then it will be cancelled. |
| 127 | |
| 128 | ev.stats.waitEnqueue.Start() |
| 129 | ev.stats.waitConsumeOffQueue.Start() |
| 130 | q.events <- ev |
| 131 | ev.stats.waitEnqueue.End(true) |
| 132 | return ev.eventResults, nil |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Event is an event that can be enqueued onto an EventQueue. |
| 137 | type Event struct { |