/
queue.go
51 lines (43 loc) · 975 Bytes
/
queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package event
import (
"errors"
"sync"
"github.com/ca-dp/bucketeer-go-server-sdk/pkg/bucketeer/model"
)
type queue struct {
evtCh chan *model.Event
closedMu sync.RWMutex
closed bool
}
type queueConfig struct {
capacity int
}
func newQueue(conf *queueConfig) *queue {
return &queue{
evtCh: make(chan *model.Event, conf.capacity),
}
}
func (q *queue) push(evt *model.Event) error {
q.closedMu.RLock()
defer q.closedMu.RUnlock()
if q.closed {
return errors.New("queue is already closed")
}
select {
case q.evtCh <- evt:
return nil
default:
// When evtCh is full, discards evt and returns an error without waiting for a space in evtCh.
// If we wait for the space, there is a risk that a user app will be a serious slowdown.
return errors.New("queue is full")
}
}
func (q *queue) eventCh() <-chan *model.Event {
return q.evtCh
}
func (q *queue) close() {
q.closedMu.Lock()
defer q.closedMu.Unlock()
close(q.evtCh)
q.closed = true
}