[50] | 1 | package jounce
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
[57] | 4 | "sync"
|
---|
| 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 | lock sync.Mutex
|
---|
| 16 | cur uint64
|
---|
| 17 | consumers []*RingConsumer
|
---|
[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) {
|
---|
[57] | 30 | r.lock.Lock()
|
---|
| 31 | defer r.lock.Unlock()
|
---|
| 32 |
|
---|
[50] | 33 | i := int(r.cur % r.cap)
|
---|
| 34 | r.buffer[i] = msg
|
---|
| 35 | r.cur++
|
---|
| 36 |
|
---|
[57] | 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 | }
|
---|
[51] | 44 | }
|
---|
[57] | 45 | }
|
---|
[51] | 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 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.
|
---|
| 57 | func (r *Ring) NewConsumer(seq *uint64) (*RingConsumer, <-chan struct{}) {
|
---|
[57] | 58 | consumer := &RingConsumer{
|
---|
[50] | 59 | ring: r,
|
---|
[57] | 60 | ch: make(chan struct{}, 1),
|
---|
[50] | 61 | }
|
---|
[57] | 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
|
---|
[50] | 76 | }
|
---|
| 77 |
|
---|
[59] | 78 | // RingConsumer is a ring buffer consumer.
|
---|
[50] | 79 | type RingConsumer struct {
|
---|
[57] | 80 | ring *Ring
|
---|
| 81 | cur uint64
|
---|
| 82 | ch chan struct{}
|
---|
| 83 | closed bool
|
---|
[50] | 84 | }
|
---|
| 85 |
|
---|
[57] | 86 | // diff returns the number of pending messages. It assumes the Ring is locked.
|
---|
| 87 | func (rc *RingConsumer) diff() uint64 {
|
---|
[50] | 88 | if rc.cur > rc.ring.cur {
|
---|
| 89 | panic("jounce: consumer cursor greater than producer cursor")
|
---|
| 90 | }
|
---|
| 91 | return rc.ring.cur - rc.cur
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[59] | 94 | // Peek returns the next pending message if any without consuming it. A nil
|
---|
| 95 | // message is returned if no message is available.
|
---|
[50] | 96 | func (rc *RingConsumer) Peek() *irc.Message {
|
---|
[57] | 97 | if rc.closed {
|
---|
| 98 | panic("jounce: RingConsumer.Peek called after Close")
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | rc.ring.lock.Lock()
|
---|
| 102 | defer rc.ring.lock.Unlock()
|
---|
| 103 |
|
---|
| 104 | diff := rc.diff()
|
---|
[50] | 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("jounce: unexpected nil ring buffer entry")
|
---|
| 116 | }
|
---|
| 117 | return msg
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[59] | 120 | // Consume consumes and returns the next pending message. A nil message is
|
---|
| 121 | // returned if no message is available.
|
---|
[50] | 122 | func (rc *RingConsumer) Consume() *irc.Message {
|
---|
| 123 | msg := rc.Peek()
|
---|
| 124 | if msg != nil {
|
---|
| 125 | rc.cur++
|
---|
| 126 | }
|
---|
| 127 | return msg
|
---|
| 128 | }
|
---|
[51] | 129 |
|
---|
[59] | 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.
|
---|
[57] | 133 | func (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
|
---|
[51] | 146 | }
|
---|