2019-08-14 20:56:45 +00:00
|
|
|
package event
|
|
|
|
|
2021-03-24 21:42:15 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2019-11-12 20:39:55 +00:00
|
|
|
|
2019-08-14 20:56:45 +00:00
|
|
|
type queue struct {
|
|
|
|
infChan infiniteChannel
|
2019-11-12 20:39:55 +00:00
|
|
|
lock sync.Mutex
|
|
|
|
closed bool
|
2019-08-14 20:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Queue is a wrapper around a channel for handling Events in a consistent way across subsystems.
|
|
|
|
// The expectation is that each subsystem in Cwtch will manage a given an event.Queue fed from
|
|
|
|
// the event.Manager.
|
|
|
|
type Queue interface {
|
2019-11-12 20:39:55 +00:00
|
|
|
Publish(event Event)
|
2021-03-24 20:09:07 +00:00
|
|
|
Next() Event
|
2019-08-14 20:56:45 +00:00
|
|
|
Shutdown()
|
2019-11-12 20:39:55 +00:00
|
|
|
OutChan() <-chan Event
|
2019-08-14 20:56:45 +00:00
|
|
|
Len() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQueue initializes an event.Queue
|
|
|
|
func NewQueue() Queue {
|
|
|
|
queue := &queue{infChan: *newInfiniteChannel()}
|
|
|
|
return queue
|
|
|
|
}
|
|
|
|
|
2019-11-12 20:39:55 +00:00
|
|
|
func (iq *queue) inChan() chan<- Event {
|
2019-08-14 20:56:45 +00:00
|
|
|
return iq.infChan.In()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iq *queue) OutChan() <-chan Event {
|
|
|
|
return iq.infChan.Out()
|
|
|
|
}
|
|
|
|
|
2023-04-17 19:05:02 +00:00
|
|
|
// Next returns the next available event from the front of the queue
|
2021-03-24 20:09:07 +00:00
|
|
|
func (iq *queue) Next() Event {
|
2019-08-14 20:56:45 +00:00
|
|
|
event := <-iq.infChan.Out()
|
2021-03-24 20:09:07 +00:00
|
|
|
return event
|
2019-08-14 20:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (iq *queue) Len() int {
|
|
|
|
return iq.infChan.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown closes our eventChannel
|
|
|
|
func (iq *queue) Shutdown() {
|
2019-11-12 20:39:55 +00:00
|
|
|
iq.lock.Lock()
|
2022-11-30 22:13:54 +00:00
|
|
|
if !iq.closed {
|
|
|
|
iq.closed = true
|
|
|
|
iq.infChan.Close()
|
|
|
|
}
|
2019-11-12 20:39:55 +00:00
|
|
|
iq.lock.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iq *queue) Publish(event Event) {
|
|
|
|
iq.lock.Lock()
|
|
|
|
if !iq.closed {
|
|
|
|
iq.inChan() <- event
|
|
|
|
}
|
|
|
|
iq.lock.Unlock()
|
2019-08-14 20:56:45 +00:00
|
|
|
}
|