[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 {
|
---|
[232] | 73 | ring *Ring
|
---|
| 74 | cur uint64
|
---|
[50] | 75 | }
|
---|
| 76 |
|
---|
[57] | 77 | // diff returns the number of pending messages. It assumes the Ring is locked.
|
---|
| 78 | func (rc *RingConsumer) diff() uint64 {
|
---|
[50] | 79 | if rc.cur > rc.ring.cur {
|
---|
[138] | 80 | panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
|
---|
[50] | 81 | }
|
---|
| 82 | return rc.ring.cur - rc.cur
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[59] | 85 | // Peek returns the next pending message if any without consuming it. A nil
|
---|
| 86 | // message is returned if no message is available.
|
---|
[50] | 87 | func (rc *RingConsumer) Peek() *irc.Message {
|
---|
[57] | 88 | diff := rc.diff()
|
---|
[50] | 89 | if diff == 0 {
|
---|
| 90 | return nil
|
---|
| 91 | }
|
---|
| 92 | if diff > rc.ring.cap {
|
---|
| 93 | // Consumer drops diff - cap entries
|
---|
| 94 | rc.cur = rc.ring.cur - rc.ring.cap
|
---|
| 95 | }
|
---|
| 96 | i := int(rc.cur % rc.ring.cap)
|
---|
| 97 | msg := rc.ring.buffer[i]
|
---|
| 98 | if msg == nil {
|
---|
[138] | 99 | panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
|
---|
[50] | 100 | }
|
---|
| 101 | return msg
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[59] | 104 | // Consume consumes and returns the next pending message. A nil message is
|
---|
| 105 | // returned if no message is available.
|
---|
[50] | 106 | func (rc *RingConsumer) Consume() *irc.Message {
|
---|
| 107 | msg := rc.Peek()
|
---|
| 108 | if msg != nil {
|
---|
| 109 | rc.cur++
|
---|
| 110 | }
|
---|
| 111 | return msg
|
---|
| 112 | }
|
---|