1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "fmt"
|
---|
5 |
|
---|
6 | "gopkg.in/irc.v3"
|
---|
7 | )
|
---|
8 |
|
---|
9 | // Ring implements a single producer, multiple consumer ring buffer. The ring
|
---|
10 | // buffer size is fixed. The ring buffer is stored in memory.
|
---|
11 | type Ring struct {
|
---|
12 | buffer []*irc.Message
|
---|
13 | cap uint64
|
---|
14 |
|
---|
15 | cur uint64
|
---|
16 | consumers []*RingConsumer
|
---|
17 | closed bool
|
---|
18 | }
|
---|
19 |
|
---|
20 | // NewRing creates a new ring buffer.
|
---|
21 | func NewRing(capacity int) *Ring {
|
---|
22 | return &Ring{
|
---|
23 | buffer: make([]*irc.Message, capacity),
|
---|
24 | cap: uint64(capacity),
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | // Produce appends a new message to the ring buffer.
|
---|
29 | func (r *Ring) Produce(msg *irc.Message) {
|
---|
30 | if r.closed {
|
---|
31 | panic("soju: Ring.Produce called after Close")
|
---|
32 | }
|
---|
33 |
|
---|
34 | i := int(r.cur % r.cap)
|
---|
35 | r.buffer[i] = msg
|
---|
36 | r.cur++
|
---|
37 | }
|
---|
38 |
|
---|
39 | func (r *Ring) Cur() uint64 {
|
---|
40 | return r.cur
|
---|
41 | }
|
---|
42 |
|
---|
43 | func (r *Ring) Close() {
|
---|
44 | if r.closed {
|
---|
45 | panic("soju: Ring.Close called twice")
|
---|
46 | }
|
---|
47 |
|
---|
48 | r.closed = true
|
---|
49 | }
|
---|
50 |
|
---|
51 | // NewConsumer creates a new ring buffer consumer.
|
---|
52 | //
|
---|
53 | // If seq is nil, the consumer will get messages starting from the last
|
---|
54 | // producer message. If seq is non-nil, the consumer will get messages starting
|
---|
55 | // from the specified history sequence number (see RingConsumer.Close).
|
---|
56 | //
|
---|
57 | // The consumer can only be used from a single goroutine.
|
---|
58 | func (r *Ring) NewConsumer(seq *uint64) *RingConsumer {
|
---|
59 | consumer := &RingConsumer{ring: r}
|
---|
60 |
|
---|
61 | if seq != nil {
|
---|
62 | consumer.cur = *seq
|
---|
63 | } else {
|
---|
64 | consumer.cur = r.cur
|
---|
65 | }
|
---|
66 | r.consumers = append(r.consumers, consumer)
|
---|
67 |
|
---|
68 | return consumer
|
---|
69 | }
|
---|
70 |
|
---|
71 | // RingConsumer is a ring buffer consumer.
|
---|
72 | type RingConsumer struct {
|
---|
73 | ring *Ring
|
---|
74 | cur uint64
|
---|
75 | closed bool
|
---|
76 | }
|
---|
77 |
|
---|
78 | // diff returns the number of pending messages. It assumes the Ring is locked.
|
---|
79 | func (rc *RingConsumer) diff() uint64 {
|
---|
80 | if rc.cur > rc.ring.cur {
|
---|
81 | panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
|
---|
82 | }
|
---|
83 | return rc.ring.cur - rc.cur
|
---|
84 | }
|
---|
85 |
|
---|
86 | // Peek returns the next pending message if any without consuming it. A nil
|
---|
87 | // message is returned if no message is available.
|
---|
88 | func (rc *RingConsumer) Peek() *irc.Message {
|
---|
89 | if rc.closed {
|
---|
90 | panic("soju: RingConsumer.Peek called after Close")
|
---|
91 | }
|
---|
92 |
|
---|
93 | diff := rc.diff()
|
---|
94 | if diff == 0 {
|
---|
95 | return nil
|
---|
96 | }
|
---|
97 | if diff > rc.ring.cap {
|
---|
98 | // Consumer drops diff - cap entries
|
---|
99 | rc.cur = rc.ring.cur - rc.ring.cap
|
---|
100 | }
|
---|
101 | i := int(rc.cur % rc.ring.cap)
|
---|
102 | msg := rc.ring.buffer[i]
|
---|
103 | if msg == nil {
|
---|
104 | panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
|
---|
105 | }
|
---|
106 | return msg
|
---|
107 | }
|
---|
108 |
|
---|
109 | // Consume consumes and returns the next pending message. A nil message is
|
---|
110 | // returned if no message is available.
|
---|
111 | func (rc *RingConsumer) Consume() *irc.Message {
|
---|
112 | msg := rc.Peek()
|
---|
113 | if msg != nil {
|
---|
114 | rc.cur++
|
---|
115 | }
|
---|
116 | return msg
|
---|
117 | }
|
---|
118 |
|
---|
119 | // Close stops consuming messages. The consumer channel will be closed. The
|
---|
120 | // current history sequence number is returned. It can be provided later as an
|
---|
121 | // argument to Ring.NewConsumer to resume the message stream.
|
---|
122 | func (rc *RingConsumer) Close() uint64 {
|
---|
123 | for i := range rc.ring.consumers {
|
---|
124 | if rc.ring.consumers[i] == rc {
|
---|
125 | rc.ring.consumers = append(rc.ring.consumers[:i], rc.ring.consumers[i+1:]...)
|
---|
126 | break
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | rc.closed = true
|
---|
131 | return rc.cur
|
---|
132 | }
|
---|