[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 |
|
---|
[203] | 39 | func (r *Ring) Close() {
|
---|
| 40 | if r.closed {
|
---|
| 41 | panic("soju: Ring.Close called twice")
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | r.closed = true
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[59] | 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.
|
---|
[228] | 54 | func (r *Ring) NewConsumer(seq *uint64) *RingConsumer {
|
---|
| 55 | consumer := &RingConsumer{ring: r}
|
---|
[57] | 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 |
|
---|
[228] | 64 | return consumer
|
---|
[50] | 65 | }
|
---|
| 66 |
|
---|
[59] | 67 | // RingConsumer is a ring buffer consumer.
|
---|
[50] | 68 | type RingConsumer struct {
|
---|
[57] | 69 | ring *Ring
|
---|
| 70 | cur uint64
|
---|
| 71 | closed bool
|
---|
[50] | 72 | }
|
---|
| 73 |
|
---|
[57] | 74 | // diff returns the number of pending messages. It assumes the Ring is locked.
|
---|
| 75 | func (rc *RingConsumer) diff() uint64 {
|
---|
[50] | 76 | if rc.cur > rc.ring.cur {
|
---|
[138] | 77 | panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
|
---|
[50] | 78 | }
|
---|
| 79 | return rc.ring.cur - rc.cur
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[59] | 82 | // Peek returns the next pending message if any without consuming it. A nil
|
---|
| 83 | // message is returned if no message is available.
|
---|
[50] | 84 | func (rc *RingConsumer) Peek() *irc.Message {
|
---|
[57] | 85 | if rc.closed {
|
---|
[98] | 86 | panic("soju: RingConsumer.Peek called after Close")
|
---|
[57] | 87 | }
|
---|
| 88 |
|
---|
| 89 | diff := rc.diff()
|
---|
[50] | 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 {
|
---|
[138] | 100 | panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
|
---|
[50] | 101 | }
|
---|
| 102 | return msg
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[59] | 105 | // Consume consumes and returns the next pending message. A nil message is
|
---|
| 106 | // returned if no message is available.
|
---|
[50] | 107 | func (rc *RingConsumer) Consume() *irc.Message {
|
---|
| 108 | msg := rc.Peek()
|
---|
| 109 | if msg != nil {
|
---|
| 110 | rc.cur++
|
---|
| 111 | }
|
---|
| 112 | return msg
|
---|
| 113 | }
|
---|
[51] | 114 |
|
---|
[59] | 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.
|
---|
[57] | 118 | func (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
|
---|
[51] | 128 | }
|
---|