mirror of
https://github.com/coder/coder.git
synced 2025-07-21 01:28:49 +00:00
feat: pubsub reports dropped messages (#7660)
* Implementation; need linux tests Signed-off-by: Spike Curtis <spike@coder.com> * Pubsub with errors tests and fixes Signed-off-by: Spike Curtis <spike@coder.com> * Deal with test goroutines Signed-off-by: Spike Curtis <spike@coder.com> --------- Signed-off-by: Spike Curtis <spike@coder.com>
This commit is contained in:
@ -7,20 +7,43 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// genericListener is either a Listener or ListenerWithErr
|
||||
type genericListener struct {
|
||||
l Listener
|
||||
le ListenerWithErr
|
||||
}
|
||||
|
||||
func (g genericListener) send(ctx context.Context, message []byte) {
|
||||
if g.l != nil {
|
||||
g.l(ctx, message)
|
||||
}
|
||||
if g.le != nil {
|
||||
g.le(ctx, message, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// memoryPubsub is an in-memory Pubsub implementation.
|
||||
type memoryPubsub struct {
|
||||
mut sync.RWMutex
|
||||
listeners map[string]map[uuid.UUID]Listener
|
||||
listeners map[string]map[uuid.UUID]genericListener
|
||||
}
|
||||
|
||||
func (m *memoryPubsub) Subscribe(event string, listener Listener) (cancel func(), err error) {
|
||||
return m.subscribeGeneric(event, genericListener{l: listener})
|
||||
}
|
||||
|
||||
func (m *memoryPubsub) SubscribeWithErr(event string, listener ListenerWithErr) (cancel func(), err error) {
|
||||
return m.subscribeGeneric(event, genericListener{le: listener})
|
||||
}
|
||||
|
||||
func (m *memoryPubsub) subscribeGeneric(event string, listener genericListener) (cancel func(), err error) {
|
||||
m.mut.Lock()
|
||||
defer m.mut.Unlock()
|
||||
|
||||
var listeners map[uuid.UUID]Listener
|
||||
var listeners map[uuid.UUID]genericListener
|
||||
var ok bool
|
||||
if listeners, ok = m.listeners[event]; !ok {
|
||||
listeners = map[uuid.UUID]Listener{}
|
||||
listeners = map[uuid.UUID]genericListener{}
|
||||
m.listeners[event] = listeners
|
||||
}
|
||||
var id uuid.UUID
|
||||
@ -52,7 +75,7 @@ func (m *memoryPubsub) Publish(event string, message []byte) error {
|
||||
listener := listener
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
listener(context.Background(), message)
|
||||
listener.send(context.Background(), message)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
@ -66,6 +89,6 @@ func (*memoryPubsub) Close() error {
|
||||
|
||||
func NewPubsubInMemory() Pubsub {
|
||||
return &memoryPubsub{
|
||||
listeners: make(map[string]map[uuid.UUID]Listener),
|
||||
listeners: make(map[string]map[uuid.UUID]genericListener),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user