Changeset 57 in code for trunk/ring.go


Ignore:
Timestamp:
Feb 17, 2020, 2:46:29 PM (5 years ago)
Author:
contact
Message:

Fix issues related to Ring

  • RingConsumer is now used directly in the goroutine responsible for writing downstream messages. This allows the ring buffer not to be consumed on write error.
  • RingConsumer now has a channel attached. This allows PRIVMSG messages to always use RingConsumer, instead of also directly pushing messages to all downstream connections.
  • Multiple clients with the same history name are now supported.
  • Ring is now protected by a mutex
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ring.go

    r51 r57  
    22
    33import (
     4        "sync"
     5
    46        "gopkg.in/irc.v3"
    57)
     
    810// buffer size is fixed. The ring buffer is stored in memory.
    911type Ring struct {
    10         buffer   []*irc.Message
    11         cap, cur uint64
     12        buffer []*irc.Message
     13        cap    uint64
    1214
    13         consumers map[string]*RingConsumer
     15        lock      sync.Mutex
     16        cur       uint64
     17        consumers []*RingConsumer
    1418}
    1519
    1620func NewRing(capacity int) *Ring {
    1721        return &Ring{
    18                 buffer:    make([]*irc.Message, capacity),
    19                 cap:       uint64(capacity),
    20                 consumers: make(map[string]*RingConsumer),
     22                buffer: make([]*irc.Message, capacity),
     23                cap:    uint64(capacity),
    2124        }
    2225}
    2326
    2427func (r *Ring) Produce(msg *irc.Message) {
     28        r.lock.Lock()
     29        defer r.lock.Unlock()
     30
    2531        i := int(r.cur % r.cap)
    2632        r.buffer[i] = msg
    2733        r.cur++
     34
     35        for _, consumer := range r.consumers {
     36                select {
     37                case consumer.ch <- struct{}{}:
     38                        // This space is intentionally left blank
     39                default:
     40                        // The channel already has a pending item
     41                }
     42        }
    2843}
    2944
    30 func (r *Ring) Consumer(name string) *RingConsumer {
    31         consumer, ok := r.consumers[name]
    32         if ok {
    33                 return consumer
     45func (r *Ring) Consumer(seq *uint64) (*RingConsumer, <-chan struct{}) {
     46        consumer := &RingConsumer{
     47                ring: r,
     48                ch:   make(chan struct{}, 1),
    3449        }
    3550
    36         consumer = &RingConsumer{
    37                 ring: r,
    38                 cur:  r.cur,
     51        r.lock.Lock()
     52        if seq != nil {
     53                consumer.cur = *seq
     54        } else {
     55                consumer.cur = r.cur
    3956        }
    40         r.consumers[name] = consumer
    41         return consumer
     57        if consumer.diff() > 0 {
     58                consumer.ch <- struct{}{}
     59        }
     60        r.consumers = append(r.consumers, consumer)
     61        r.lock.Unlock()
     62
     63        return consumer, consumer.ch
    4264}
    4365
    4466type RingConsumer struct {
    45         ring *Ring
    46         cur  uint64
     67        ring   *Ring
     68        cur    uint64
     69        ch     chan struct{}
     70        closed bool
    4771}
    4872
    49 func (rc *RingConsumer) Diff() uint64 {
     73// diff returns the number of pending messages. It assumes the Ring is locked.
     74func (rc *RingConsumer) diff() uint64 {
    5075        if rc.cur > rc.ring.cur {
    5176                panic("jounce: consumer cursor greater than producer cursor")
     
    5580
    5681func (rc *RingConsumer) Peek() *irc.Message {
    57         diff := rc.Diff()
     82        if rc.closed {
     83                panic("jounce: RingConsumer.Peek called after Close")
     84        }
     85
     86        rc.ring.lock.Lock()
     87        defer rc.ring.lock.Unlock()
     88
     89        diff := rc.diff()
    5890        if diff == 0 {
    5991                return nil
     
    79111}
    80112
    81 func (rc *RingConsumer) Reset() {
    82         rc.cur = rc.ring.cur
     113func (rc *RingConsumer) Close() uint64 {
     114        rc.ring.lock.Lock()
     115        for i := range rc.ring.consumers {
     116                if rc.ring.consumers[i] == rc {
     117                        rc.ring.consumers = append(rc.ring.consumers[:i], rc.ring.consumers[i+1:]...)
     118                        break
     119                }
     120        }
     121        rc.ring.lock.Unlock()
     122
     123        close(rc.ch)
     124        rc.closed = true
     125        return rc.cur
    83126}
Note: See TracChangeset for help on using the changeset viewer.