2020-03-13 17:13:03 +00:00
|
|
|
package soju
|
2020-02-07 15:43:14 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-24 08:21:49 +00:00
|
|
|
"fmt"
|
2020-02-17 14:46:29 +00:00
|
|
|
|
2020-02-07 15:43:14 +00:00
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ring implements a single producer, multiple consumer ring buffer. The ring
|
|
|
|
// buffer size is fixed. The ring buffer is stored in memory.
|
|
|
|
type Ring struct {
|
2020-02-17 14:46:29 +00:00
|
|
|
buffer []*irc.Message
|
|
|
|
cap uint64
|
2020-02-07 15:43:14 +00:00
|
|
|
|
2020-02-17 14:46:29 +00:00
|
|
|
cur uint64
|
|
|
|
consumers []*RingConsumer
|
2020-04-01 13:48:56 +00:00
|
|
|
closed bool
|
2020-02-07 15:43:14 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 15:09:35 +00:00
|
|
|
// NewRing creates a new ring buffer.
|
2020-02-07 15:43:14 +00:00
|
|
|
func NewRing(capacity int) *Ring {
|
|
|
|
return &Ring{
|
2020-02-17 14:46:29 +00:00
|
|
|
buffer: make([]*irc.Message, capacity),
|
|
|
|
cap: uint64(capacity),
|
2020-02-07 15:43:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:09:35 +00:00
|
|
|
// Produce appends a new message to the ring buffer.
|
2020-02-07 15:43:14 +00:00
|
|
|
func (r *Ring) Produce(msg *irc.Message) {
|
2020-04-01 13:48:56 +00:00
|
|
|
if r.closed {
|
|
|
|
panic("soju: Ring.Produce called after Close")
|
|
|
|
}
|
|
|
|
|
2020-02-07 15:43:14 +00:00
|
|
|
i := int(r.cur % r.cap)
|
|
|
|
r.buffer[i] = msg
|
|
|
|
r.cur++
|
2020-02-17 14:46:29 +00:00
|
|
|
}
|
2020-02-07 16:15:50 +00:00
|
|
|
|
2020-04-06 16:31:48 +00:00
|
|
|
func (r *Ring) Cur() uint64 {
|
|
|
|
return r.cur
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:48:56 +00:00
|
|
|
func (r *Ring) Close() {
|
|
|
|
if r.closed {
|
|
|
|
panic("soju: Ring.Close called twice")
|
|
|
|
}
|
|
|
|
|
|
|
|
r.closed = true
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:09:35 +00:00
|
|
|
// NewConsumer creates a new ring buffer consumer.
|
|
|
|
//
|
|
|
|
// If seq is nil, the consumer will get messages starting from the last
|
|
|
|
// producer message. If seq is non-nil, the consumer will get messages starting
|
|
|
|
// from the specified history sequence number (see RingConsumer.Close).
|
|
|
|
//
|
|
|
|
// The consumer can only be used from a single goroutine.
|
2020-04-06 16:13:46 +00:00
|
|
|
func (r *Ring) NewConsumer(seq *uint64) *RingConsumer {
|
|
|
|
consumer := &RingConsumer{ring: r}
|
2020-02-17 14:46:29 +00:00
|
|
|
|
|
|
|
if seq != nil {
|
|
|
|
consumer.cur = *seq
|
|
|
|
} else {
|
|
|
|
consumer.cur = r.cur
|
|
|
|
}
|
|
|
|
r.consumers = append(r.consumers, consumer)
|
|
|
|
|
2020-04-06 16:13:46 +00:00
|
|
|
return consumer
|
2020-02-07 15:43:14 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 15:09:35 +00:00
|
|
|
// RingConsumer is a ring buffer consumer.
|
2020-02-07 15:43:14 +00:00
|
|
|
type RingConsumer struct {
|
2020-04-06 16:33:26 +00:00
|
|
|
ring *Ring
|
|
|
|
cur uint64
|
2020-02-07 15:43:14 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 14:46:29 +00:00
|
|
|
// diff returns the number of pending messages. It assumes the Ring is locked.
|
|
|
|
func (rc *RingConsumer) diff() uint64 {
|
2020-02-07 15:43:14 +00:00
|
|
|
if rc.cur > rc.ring.cur {
|
2020-03-24 08:21:49 +00:00
|
|
|
panic(fmt.Sprintf("soju: consumer cursor (%v) greater than producer cursor (%v)", rc.cur, rc.ring.cur))
|
2020-02-07 15:43:14 +00:00
|
|
|
}
|
|
|
|
return rc.ring.cur - rc.cur
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:09:35 +00:00
|
|
|
// Peek returns the next pending message if any without consuming it. A nil
|
|
|
|
// message is returned if no message is available.
|
2020-02-07 15:43:14 +00:00
|
|
|
func (rc *RingConsumer) Peek() *irc.Message {
|
2020-02-17 14:46:29 +00:00
|
|
|
diff := rc.diff()
|
2020-02-07 15:43:14 +00:00
|
|
|
if diff == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if diff > rc.ring.cap {
|
|
|
|
// Consumer drops diff - cap entries
|
|
|
|
rc.cur = rc.ring.cur - rc.ring.cap
|
|
|
|
}
|
|
|
|
i := int(rc.cur % rc.ring.cap)
|
|
|
|
msg := rc.ring.buffer[i]
|
|
|
|
if msg == nil {
|
2020-03-24 08:21:49 +00:00
|
|
|
panic(fmt.Sprintf("soju: unexpected nil ring buffer entry at index %v", i))
|
2020-02-07 15:43:14 +00:00
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:09:35 +00:00
|
|
|
// Consume consumes and returns the next pending message. A nil message is
|
|
|
|
// returned if no message is available.
|
2020-02-07 15:43:14 +00:00
|
|
|
func (rc *RingConsumer) Consume() *irc.Message {
|
|
|
|
msg := rc.Peek()
|
|
|
|
if msg != nil {
|
|
|
|
rc.cur++
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|