source: code/trunk/ring.go@ 98

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

Rename project to soju

File size: 3.4 KB
Line 
1package soju
2
3import (
4 "sync"
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 lock sync.Mutex
16 cur uint64
17 consumers []*RingConsumer
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 r.lock.Lock()
31 defer r.lock.Unlock()
32
33 i := int(r.cur % r.cap)
34 r.buffer[i] = msg
35 r.cur++
36
37 for _, consumer := range r.consumers {
38 select {
39 case consumer.ch <- struct{}{}:
40 // This space is intentionally left blank
41 default:
42 // The channel already has a pending item
43 }
44 }
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 returned channel yields a value each time the consumer has a new message
54// available. Consume should be called to drain the consumer.
55//
56// The consumer can only be used from a single goroutine.
57func (r *Ring) NewConsumer(seq *uint64) (*RingConsumer, <-chan struct{}) {
58 consumer := &RingConsumer{
59 ring: r,
60 ch: make(chan struct{}, 1),
61 }
62
63 r.lock.Lock()
64 if seq != nil {
65 consumer.cur = *seq
66 } else {
67 consumer.cur = r.cur
68 }
69 if consumer.diff() > 0 {
70 consumer.ch <- struct{}{}
71 }
72 r.consumers = append(r.consumers, consumer)
73 r.lock.Unlock()
74
75 return consumer, consumer.ch
76}
77
78// RingConsumer is a ring buffer consumer.
79type RingConsumer struct {
80 ring *Ring
81 cur uint64
82 ch chan struct{}
83 closed bool
84}
85
86// diff returns the number of pending messages. It assumes the Ring is locked.
87func (rc *RingConsumer) diff() uint64 {
88 if rc.cur > rc.ring.cur {
89 panic("soju: consumer cursor greater than producer cursor")
90 }
91 return rc.ring.cur - rc.cur
92}
93
94// Peek returns the next pending message if any without consuming it. A nil
95// message is returned if no message is available.
96func (rc *RingConsumer) Peek() *irc.Message {
97 if rc.closed {
98 panic("soju: RingConsumer.Peek called after Close")
99 }
100
101 rc.ring.lock.Lock()
102 defer rc.ring.lock.Unlock()
103
104 diff := rc.diff()
105 if diff == 0 {
106 return nil
107 }
108 if diff > rc.ring.cap {
109 // Consumer drops diff - cap entries
110 rc.cur = rc.ring.cur - rc.ring.cap
111 }
112 i := int(rc.cur % rc.ring.cap)
113 msg := rc.ring.buffer[i]
114 if msg == nil {
115 panic("soju: unexpected nil ring buffer entry")
116 }
117 return msg
118}
119
120// Consume consumes and returns the next pending message. A nil message is
121// returned if no message is available.
122func (rc *RingConsumer) Consume() *irc.Message {
123 msg := rc.Peek()
124 if msg != nil {
125 rc.cur++
126 }
127 return msg
128}
129
130// Close stops consuming messages. The consumer channel will be closed. The
131// current history sequence number is returned. It can be provided later as an
132// argument to Ring.NewConsumer to resume the message stream.
133func (rc *RingConsumer) Close() uint64 {
134 rc.ring.lock.Lock()
135 for i := range rc.ring.consumers {
136 if rc.ring.consumers[i] == rc {
137 rc.ring.consumers = append(rc.ring.consumers[:i], rc.ring.consumers[i+1:]...)
138 break
139 }
140 }
141 rc.ring.lock.Unlock()
142
143 close(rc.ch)
144 rc.closed = true
145 return rc.cur
146}
Note: See TracBrowser for help on using the repository browser.