source: code/trunk/ring.go@ 230

Last change on this file since 230 was 229, checked in by contact, 5 years ago

Remove locks from ring buffer

Everything is now accessed from the user goroutine.

File size: 3.0 KB
Line 
1package soju
2
3import (
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.
11type 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.
21func 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.
29func (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
39func (r *Ring) Close() {
40 if r.closed {
41 panic("soju: Ring.Close called twice")
42 }
43
44 r.closed = true
45}
46
47// NewConsumer creates a new ring buffer consumer.
48//
49// If seq is nil, the consumer will get messages starting from the last
50// producer message. If seq is non-nil, the consumer will get messages starting
51// from the specified history sequence number (see RingConsumer.Close).
52//
53// The consumer can only be used from a single goroutine.
54func (r *Ring) NewConsumer(seq *uint64) *RingConsumer {
55 consumer := &RingConsumer{ring: r}
56
57 if seq != nil {
58 consumer.cur = *seq
59 } else {
60 consumer.cur = r.cur
61 }
62 r.consumers = append(r.consumers, consumer)
63
64 return consumer
65}
66
67// RingConsumer is a ring buffer consumer.
68type RingConsumer struct {
69 ring *Ring
70 cur uint64
71 closed bool
72}
73
74// diff returns the number of pending messages. It assumes the Ring is locked.
75func (rc *RingConsumer) diff() uint64 {
76 if rc.cur > rc.ring.cur {
77 panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
78 }
79 return rc.ring.cur - rc.cur
80}
81
82// Peek returns the next pending message if any without consuming it. A nil
83// message is returned if no message is available.
84func (rc *RingConsumer) Peek() *irc.Message {
85 if rc.closed {
86 panic("soju: RingConsumer.Peek called after Close")
87 }
88
89 diff := rc.diff()
90 if diff == 0 {
91 return nil
92 }
93 if diff > rc.ring.cap {
94 // Consumer drops diff - cap entries
95 rc.cur = rc.ring.cur - rc.ring.cap
96 }
97 i := int(rc.cur % rc.ring.cap)
98 msg := rc.ring.buffer[i]
99 if msg == nil {
100 panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
101 }
102 return msg
103}
104
105// Consume consumes and returns the next pending message. A nil message is
106// returned if no message is available.
107func (rc *RingConsumer) Consume() *irc.Message {
108 msg := rc.Peek()
109 if msg != nil {
110 rc.cur++
111 }
112 return msg
113}
114
115// Close stops consuming messages. The consumer channel will be closed. The
116// current history sequence number is returned. It can be provided later as an
117// argument to Ring.NewConsumer to resume the message stream.
118func (rc *RingConsumer) Close() uint64 {
119 for i := range rc.ring.consumers {
120 if rc.ring.consumers[i] == rc {
121 rc.ring.consumers = append(rc.ring.consumers[:i], rc.ring.consumers[i+1:]...)
122 break
123 }
124 }
125
126 rc.closed = true
127 return rc.cur
128}
Note: See TracBrowser for help on using the repository browser.