Add names to consumers

This commit is contained in:
Simon Ser 2020-02-07 17:15:50 +01:00
parent fad9d820c1
commit 7127fa325a
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 28 additions and 8 deletions

View File

@ -128,6 +128,12 @@ func (c *downstreamConn) Close() error {
}
}
u.lock.Unlock()
// TODO: figure out a better way to advance the ring buffer consumer cursor
u.forEachUpstream(func(uc *upstreamConn) {
// TODO: let clients specify the ring buffer name in their username
uc.ring.Consumer("").Reset()
})
}
close(c.messages)
@ -232,8 +238,10 @@ func (c *downstreamConn) register() error {
}
}
consumer := uc.ring.Consumer()
// TODO: let clients specify the ring buffer name in their username
consumer := uc.ring.Consumer("")
for {
// TODO: these messages will get lost if the connection is closed
msg := consumer.Consume()
if msg == nil {
break

22
ring.go
View File

@ -10,13 +10,14 @@ type Ring struct {
buffer []*irc.Message
cap, cur uint64
consumers []RingConsumer
consumers map[string]*RingConsumer
}
func NewRing(capacity int) *Ring {
return &Ring{
buffer: make([]*irc.Message, capacity),
cap: uint64(capacity),
consumers: make(map[string]*RingConsumer),
}
}
@ -26,11 +27,18 @@ func (r *Ring) Produce(msg *irc.Message) {
r.cur++
}
func (r *Ring) Consumer() *RingConsumer {
return &RingConsumer{
ring: r,
cur: 0, // r.cur
func (r *Ring) Consumer(name string) *RingConsumer {
consumer, ok := r.consumers[name]
if ok {
return consumer
}
consumer = &RingConsumer{
ring: r,
cur: r.cur,
}
r.consumers[name] = consumer
return consumer
}
type RingConsumer struct {
@ -69,3 +77,7 @@ func (rc *RingConsumer) Consume() *irc.Message {
}
return msg
}
func (rc *RingConsumer) Reset() {
rc.cur = rc.ring.cur
}