[98] | 1 | package soju
|
---|
[50] | 2 |
|
---|
| 3 | import (
|
---|
[138] | 4 | "fmt"
|
---|
[57] | 5 |
|
---|
[50] | 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 {
|
---|
[57] | 12 | buffer []*irc.Message
|
---|
| 13 | cap uint64
|
---|
[50] | 14 |
|
---|
[57] | 15 | cur uint64
|
---|
| 16 | consumers []*RingConsumer
|
---|
[203] | 17 | closed bool
|
---|
[50] | 18 | }
|
---|
| 19 |
|
---|
[59] | 20 | // NewRing creates a new ring buffer.
|
---|
[50] | 21 | func NewRing(capacity int) *Ring {
|
---|
| 22 | return &Ring{
|
---|
[57] | 23 | buffer: make([]*irc.Message, capacity),
|
---|
| 24 | cap: uint64(capacity),
|
---|
[50] | 25 | }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[59] | 28 | // Produce appends a new message to the ring buffer.
|
---|
[50] | 29 | func (r *Ring) Produce(msg *irc.Message) {
|
---|
[203] | 30 | if r.closed {
|
---|
| 31 | panic("soju: Ring.Produce called after Close")
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[50] | 34 | i := int(r.cur % r.cap)
|
---|
| 35 | r.buffer[i] = msg
|
---|
| 36 | r.cur++
|
---|
[57] | 37 | }
|
---|
[51] | 38 |
|
---|
[231] | 39 | func (r *Ring) Cur() uint64 {
|
---|
| 40 | return r.cur
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[203] | 43 | func (r *Ring) Close() {
|
---|
| 44 | if r.closed {
|
---|
| 45 | panic("soju: Ring.Close called twice")
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | r.closed = true
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[59] | 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.
|
---|
[228] | 58 | func (r *Ring) NewConsumer(seq *uint64) *RingConsumer {
|
---|
| 59 | consumer := &RingConsumer{ring: r}
|
---|
[57] | 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 |
|
---|
[228] | 68 | return consumer
|
---|
[50] | 69 | }
|
---|
| 70 |
|
---|
[59] | 71 | // RingConsumer is a ring buffer consumer.
|
---|
[50] | 72 | type RingConsumer struct {
|
---|
[57] | 73 | ring *Ring
|
---|
| 74 | cur uint64
|
---|
| 75 | closed bool
|
---|
[50] | 76 | }
|
---|
| 77 |
|
---|
[57] | 78 | // diff returns the number of pending messages. It assumes the Ring is locked.
|
---|
| 79 | func (rc *RingConsumer) diff() uint64 {
|
---|
[50] | 80 | if rc.cur > rc.ring.cur {
|
---|
[138] | 81 | panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
|
---|
[50] | 82 | }
|
---|
| 83 | return rc.ring.cur - rc.cur
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[59] | 86 | // Peek returns the next pending message if any without consuming it. A nil
|
---|
| 87 | // message is returned if no message is available.
|
---|
[50] | 88 | func (rc *RingConsumer) Peek() *irc.Message {
|
---|
[57] | 89 | if rc.closed {
|
---|
[98] | 90 | panic("soju: RingConsumer.Peek called after Close")
|
---|
[57] | 91 | }
|
---|
| 92 |
|
---|
| 93 | diff := rc.diff()
|
---|
[50] | 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 {
|
---|
[138] | 104 | panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
|
---|
[50] | 105 | }
|
---|
| 106 | return msg
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[59] | 109 | // Consume consumes and returns the next pending message. A nil message is
|
---|
| 110 | // returned if no message is available.
|
---|
[50] | 111 | func (rc *RingConsumer) Consume() *irc.Message {
|
---|
| 112 | msg := rc.Peek()
|
---|
| 113 | if msg != nil {
|
---|
| 114 | rc.cur++
|
---|
| 115 | }
|
---|
| 116 | return msg
|
---|
| 117 | }
|
---|
[51] | 118 |
|
---|
[59] | 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.
|
---|
[57] | 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
|
---|
[51] | 132 | }
|
---|