Changeset 59 in code for trunk


Ignore:
Timestamp:
Feb 17, 2020, 3:09:35 PM (5 years ago)
Author:
contact
Message:

Document Ring

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/downstream.go

    r58 r59  
    288288                }
    289289
    290                 consumer, ch := uc.ring.Consumer(seqPtr)
     290                consumer, ch := uc.ring.NewConsumer(seqPtr)
    291291                go func() {
    292292                        for {
  • trunk/ring.go

    r57 r59  
    1818}
    1919
     20// NewRing creates a new ring buffer.
    2021func NewRing(capacity int) *Ring {
    2122        return &Ring{
     
    2526}
    2627
     28// Produce appends a new message to the ring buffer.
    2729func (r *Ring) Produce(msg *irc.Message) {
    2830        r.lock.Lock()
     
    4345}
    4446
    45 func (r *Ring) Consumer(seq *uint64) (*RingConsumer, <-chan struct{}) {
     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.
     57func (r *Ring) NewConsumer(seq *uint64) (*RingConsumer, <-chan struct{}) {
    4658        consumer := &RingConsumer{
    4759                ring: r,
     
    6476}
    6577
     78// RingConsumer is a ring buffer consumer.
    6679type RingConsumer struct {
    6780        ring   *Ring
     
    7992}
    8093
     94// Peek returns the next pending message if any without consuming it. A nil
     95// message is returned if no message is available.
    8196func (rc *RingConsumer) Peek() *irc.Message {
    8297        if rc.closed {
     
    103118}
    104119
     120// Consume consumes and returns the next pending message. A nil message is
     121// returned if no message is available.
    105122func (rc *RingConsumer) Consume() *irc.Message {
    106123        msg := rc.Peek()
     
    111128}
    112129
     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.
    113133func (rc *RingConsumer) Close() uint64 {
    114134        rc.ring.lock.Lock()
Note: See TracChangeset for help on using the changeset viewer.