fix inbox sub close panic on envoy

This commit is contained in:
Jay
2026-05-18 16:44:46 -04:00
parent 62df05e01d
commit 6066639863
2 changed files with 38 additions and 27 deletions
+11 -10
View File
@@ -77,7 +77,8 @@ type Envoy struct {
events <-chan OutboundPoolEvent events <-chan OutboundPoolEvent
inbox <-chan InboxMessage inbox <-chan InboxMessage
eventSubs []chan<- OutboundPoolEvent eventSubs []chan<- OutboundPoolEvent
inboxSubs map[string][]chan<- InboxMessage labelledInboxSubs map[string][]chan<- InboxMessage
inboxSubs []chan<- InboxMessage
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
@@ -318,7 +319,7 @@ func newEnvoy(
send: send, send: send,
events: events, events: events,
inbox: inbox, inbox: inbox,
inboxSubs: make(map[string][]chan<- InboxMessage), labelledInboxSubs: make(map[string][]chan<- InboxMessage),
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
} }
@@ -362,14 +363,13 @@ func (e *Envoy) Dismiss() {
close(sub) close(sub)
} }
for _, subs := range e.inboxSubs { for _, sub := range e.inboxSubs {
for _, sub := range subs {
close(sub) close(sub)
} }
}
e.eventSubs = nil e.eventSubs = nil
e.inboxSubs = make(map[string][]chan<- InboxMessage) e.inboxSubs = nil
e.labelledInboxSubs = make(map[string][]chan<- InboxMessage)
} }
func (e *Envoy) Send(data []byte) error { func (e *Envoy) Send(data []byte) error {
@@ -388,11 +388,12 @@ func (e *Envoy) SubscribeInbox(labels []string) <-chan InboxMessage {
e.mu.Lock() e.mu.Lock()
defer e.mu.Unlock() defer e.mu.Unlock()
ch := make(chan InboxMessage) ch := make(chan InboxMessage)
e.inboxSubs = append(e.inboxSubs, ch)
for _, label := range labels { for _, label := range labels {
if _, ok := e.inboxSubs[label]; !ok { if _, ok := e.labelledInboxSubs[label]; !ok {
e.inboxSubs[label] = make([]chan<- InboxMessage, 0) e.labelledInboxSubs[label] = make([]chan<- InboxMessage, 0)
} }
e.inboxSubs[label] = append(e.inboxSubs[label], ch) e.labelledInboxSubs[label] = append(e.labelledInboxSubs[label], ch)
} }
return ch return ch
} }
@@ -448,7 +449,7 @@ func (e *Envoy) routeInbox() {
} }
e.mu.RLock() e.mu.RLock()
subs, ok := e.inboxSubs[string(label)] subs, ok := e.labelledInboxSubs[string(label)]
e.mu.RUnlock() e.mu.RUnlock()
if !ok { if !ok {
+10
View File
@@ -22,11 +22,21 @@ func TestEnvoy_Dismiss(t *testing.T) {
} }
envoy := newEnvoy(ctx, url, terminate, nil, nil, nil, nil) envoy := newEnvoy(ctx, url, terminate, nil, nil, nil, nil)
eventSub := envoy.SubscribeEvents()
inboxSub := envoy.SubscribeInbox([]string{"A", "B"})
envoy.Dismiss() envoy.Dismiss()
mu.RLock() mu.RLock()
defer mu.RUnlock() defer mu.RUnlock()
assert.True(t, terminated) assert.True(t, terminated)
_, ok := <-eventSub
assert.False(t, ok)
_, ok = <-inboxSub
assert.False(t, ok)
} }
func TestEnvoy_Send(t *testing.T) { func TestEnvoy_Send(t *testing.T) {