[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
|
---|
[50] | 17 | }
|
---|
| 18 |
|
---|
[59] | 19 | // NewRing creates a new ring buffer.
|
---|
[50] | 20 | func NewRing(capacity int) *Ring {
|
---|
| 21 | return &Ring{
|
---|
[57] | 22 | buffer: make([]*irc.Message, capacity),
|
---|
| 23 | cap: uint64(capacity),
|
---|
[50] | 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
[59] | 27 | // Produce appends a new message to the ring buffer.
|
---|
[50] | 28 | func (r *Ring) Produce(msg *irc.Message) {
|
---|
| 29 | i := int(r.cur % r.cap)
|
---|
| 30 | r.buffer[i] = msg
|
---|
| 31 | r.cur++
|
---|
[57] | 32 | }
|
---|
[51] | 33 |
|
---|
[242] | 34 | // Cur returns the current history sequence number.
|
---|
[231] | 35 | func (r *Ring) Cur() uint64 {
|
---|
| 36 | return r.cur
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[59] | 39 | // NewConsumer creates a new ring buffer consumer.
|
---|
| 40 | //
|
---|
[242] | 41 | // The consumer will get messages starting from the specified history sequence
|
---|
| 42 | // number (see Ring.Cur).
|
---|
| 43 | func (r *Ring) NewConsumer(seq uint64) *RingConsumer {
|
---|
| 44 | consumer := &RingConsumer{ring: r, cur: seq}
|
---|
[57] | 45 | r.consumers = append(r.consumers, consumer)
|
---|
[228] | 46 | return consumer
|
---|
[50] | 47 | }
|
---|
| 48 |
|
---|
[59] | 49 | // RingConsumer is a ring buffer consumer.
|
---|
[50] | 50 | type RingConsumer struct {
|
---|
[232] | 51 | ring *Ring
|
---|
| 52 | cur uint64
|
---|
[50] | 53 | }
|
---|
| 54 |
|
---|
[57] | 55 | // diff returns the number of pending messages. It assumes the Ring is locked.
|
---|
| 56 | func (rc *RingConsumer) diff() uint64 {
|
---|
[50] | 57 | if rc.cur > rc.ring.cur {
|
---|
[138] | 58 | panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
|
---|
[50] | 59 | }
|
---|
| 60 | return rc.ring.cur - rc.cur
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[59] | 63 | // Peek returns the next pending message if any without consuming it. A nil
|
---|
| 64 | // message is returned if no message is available.
|
---|
[50] | 65 | func (rc *RingConsumer) Peek() *irc.Message {
|
---|
[57] | 66 | diff := rc.diff()
|
---|
[50] | 67 | if diff == 0 {
|
---|
| 68 | return nil
|
---|
| 69 | }
|
---|
| 70 | if diff > rc.ring.cap {
|
---|
| 71 | // Consumer drops diff - cap entries
|
---|
| 72 | rc.cur = rc.ring.cur - rc.ring.cap
|
---|
| 73 | }
|
---|
| 74 | i := int(rc.cur % rc.ring.cap)
|
---|
| 75 | msg := rc.ring.buffer[i]
|
---|
| 76 | if msg == nil {
|
---|
[138] | 77 | panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
|
---|
[50] | 78 | }
|
---|
| 79 | return msg
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[59] | 82 | // Consume consumes and returns the next pending message. A nil message is
|
---|
| 83 | // returned if no message is available.
|
---|
[50] | 84 | func (rc *RingConsumer) Consume() *irc.Message {
|
---|
| 85 | msg := rc.Peek()
|
---|
| 86 | if msg != nil {
|
---|
| 87 | rc.cur++
|
---|
| 88 | }
|
---|
| 89 | return msg
|
---|
| 90 | }
|
---|