source: code/trunk/ring.go@ 216

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

Stop ring consumers when deleting network

File size: 3.7 KB
RevLine 
[98]1package soju
[50]2
3import (
[138]4 "fmt"
[57]5 "sync"
6
[50]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 {
[57]13 buffer []*irc.Message
14 cap uint64
[50]15
[57]16 lock sync.Mutex
17 cur uint64
18 consumers []*RingConsumer
[203]19 closed bool
[50]20}
21
[59]22// NewRing creates a new ring buffer.
[50]23func NewRing(capacity int) *Ring {
24 return &Ring{
[57]25 buffer: make([]*irc.Message, capacity),
26 cap: uint64(capacity),
[50]27 }
28}
29
[59]30// Produce appends a new message to the ring buffer.
[50]31func (r *Ring) Produce(msg *irc.Message) {
[57]32 r.lock.Lock()
33 defer r.lock.Unlock()
34
[203]35 if r.closed {
36 panic("soju: Ring.Produce called after Close")
37 }
38
[50]39 i := int(r.cur % r.cap)
40 r.buffer[i] = msg
41 r.cur++
42
[57]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 }
[51]50 }
[57]51}
[51]52
[203]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
[59]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{}) {
[57]79 consumer := &RingConsumer{
[50]80 ring: r,
[57]81 ch: make(chan struct{}, 1),
[50]82 }
[57]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
[50]97}
98
[59]99// RingConsumer is a ring buffer consumer.
[50]100type RingConsumer struct {
[57]101 ring *Ring
102 cur uint64
103 ch chan struct{}
104 closed bool
[50]105}
106
[57]107// diff returns the number of pending messages. It assumes the Ring is locked.
108func (rc *RingConsumer) diff() uint64 {
[50]109 if rc.cur > rc.ring.cur {
[138]110 panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
[50]111 }
112 return rc.ring.cur - rc.cur
113}
114
[59]115// Peek returns the next pending message if any without consuming it. A nil
116// message is returned if no message is available.
[50]117func (rc *RingConsumer) Peek() *irc.Message {
[57]118 if rc.closed {
[98]119 panic("soju: RingConsumer.Peek called after Close")
[57]120 }
121
122 rc.ring.lock.Lock()
123 defer rc.ring.lock.Unlock()
124
125 diff := rc.diff()
[50]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 {
[138]136 panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
[50]137 }
138 return msg
139}
140
[59]141// Consume consumes and returns the next pending message. A nil message is
142// returned if no message is available.
[50]143func (rc *RingConsumer) Consume() *irc.Message {
144 msg := rc.Peek()
145 if msg != nil {
146 rc.cur++
147 }
148 return msg
149}
[51]150
[59]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.
[57]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
[51]167}
Note: See TracBrowser for help on using the repository browser.