source: code/trunk/ring.go@ 226

Last change on this file since 226 was 203, checked in by contact, 5 years ago

Stop ring consumers when deleting network

File size: 3.7 KB
Line 
1package soju
2
3import (
4 "fmt"
5 "sync"
6
7 "gopkg.in/irc.v3"
8)
9
10// Ring implements a single producer, multiple consumer ring buffer. The ring
11// buffer size is fixed. The ring buffer is stored in memory.
12type Ring struct {
13 buffer []*irc.Message
14 cap uint64
15
16 lock sync.Mutex
17 cur uint64
18 consumers []*RingConsumer
19 closed bool
20}
21
22// NewRing creates a new ring buffer.
23func NewRing(capacity int) *Ring {
24 return &Ring{
25 buffer: make([]*irc.Message, capacity),
26 cap: uint64(capacity),
27 }
28}
29
30// Produce appends a new message to the ring buffer.
31func (r *Ring) Produce(msg *irc.Message) {
32 r.lock.Lock()
33 defer r.lock.Unlock()
34
35 if r.closed {
36 panic("soju: Ring.Produce called after Close")
37 }
38
39 i := int(r.cur % r.cap)
40 r.buffer[i] = msg
41 r.cur++
42
43 for _, consumer := range r.consumers {
44 select {
45 case consumer.ch <- struct{}{}:
46 // This space is intentionally left blank
47 default:
48 // The channel already has a pending item
49 }
50 }
51}
52
53func (r *Ring) Close() {
54 r.lock.Lock()
55 defer r.lock.Unlock()
56
57 if r.closed {
58 panic("soju: Ring.Close called twice")
59 }
60
61 for _, rc := range r.consumers {
62 close(rc.ch)
63 }
64
65 r.closed = true
66}
67
68// NewConsumer creates a new ring buffer consumer.
69//
70// If seq is nil, the consumer will get messages starting from the last
71// producer message. If seq is non-nil, the consumer will get messages starting
72// from the specified history sequence number (see RingConsumer.Close).
73//
74// The returned channel yields a value each time the consumer has a new message
75// available. Consume should be called to drain the consumer.
76//
77// The consumer can only be used from a single goroutine.
78func (r *Ring) NewConsumer(seq *uint64) (*RingConsumer, <-chan struct{}) {
79 consumer := &RingConsumer{
80 ring: r,
81 ch: make(chan struct{}, 1),
82 }
83
84 r.lock.Lock()
85 if seq != nil {
86 consumer.cur = *seq
87 } else {
88 consumer.cur = r.cur
89 }
90 if consumer.diff() > 0 {
91 consumer.ch <- struct{}{}
92 }
93 r.consumers = append(r.consumers, consumer)
94 r.lock.Unlock()
95
96 return consumer, consumer.ch
97}
98
99// RingConsumer is a ring buffer consumer.
100type RingConsumer struct {
101 ring *Ring
102 cur uint64
103 ch chan struct{}
104 closed bool
105}
106
107// diff returns the number of pending messages. It assumes the Ring is locked.
108func (rc *RingConsumer) diff() uint64 {
109 if rc.cur > rc.ring.cur {
110 panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
111 }
112 return rc.ring.cur - rc.cur
113}
114
115// Peek returns the next pending message if any without consuming it. A nil
116// message is returned if no message is available.
117func (rc *RingConsumer) Peek() *irc.Message {
118 if rc.closed {
119 panic("soju: RingConsumer.Peek called after Close")
120 }
121
122 rc.ring.lock.Lock()
123 defer rc.ring.lock.Unlock()
124
125 diff := rc.diff()
126 if diff == 0 {
127 return nil
128 }
129 if diff > rc.ring.cap {
130 // Consumer drops diff - cap entries
131 rc.cur = rc.ring.cur - rc.ring.cap
132 }
133 i := int(rc.cur % rc.ring.cap)
134 msg := rc.ring.buffer[i]
135 if msg == nil {
136 panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
137 }
138 return msg
139}
140
141// Consume consumes and returns the next pending message. A nil message is
142// returned if no message is available.
143func (rc *RingConsumer) Consume() *irc.Message {
144 msg := rc.Peek()
145 if msg != nil {
146 rc.cur++
147 }
148 return msg
149}
150
151// Close stops consuming messages. The consumer channel will be closed. The
152// current history sequence number is returned. It can be provided later as an
153// argument to Ring.NewConsumer to resume the message stream.
154func (rc *RingConsumer) Close() uint64 {
155 rc.ring.lock.Lock()
156 for i := range rc.ring.consumers {
157 if rc.ring.consumers[i] == rc {
158 rc.ring.consumers = append(rc.ring.consumers[:i], rc.ring.consumers[i+1:]...)
159 break
160 }
161 }
162 rc.ring.lock.Unlock()
163
164 close(rc.ch)
165 rc.closed = true
166 return rc.cur
167}
Note: See TracBrowser for help on using the repository browser.