fix inbox sub close panic on envoy
This commit is contained in:
+11
-10
@@ -77,7 +77,8 @@ type Envoy struct {
|
||||
events <-chan OutboundPoolEvent
|
||||
inbox <-chan InboxMessage
|
||||
eventSubs []chan<- OutboundPoolEvent
|
||||
inboxSubs map[string][]chan<- InboxMessage
|
||||
labelledInboxSubs map[string][]chan<- InboxMessage
|
||||
inboxSubs []chan<- InboxMessage
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
@@ -318,7 +319,7 @@ func newEnvoy(
|
||||
send: send,
|
||||
events: events,
|
||||
inbox: inbox,
|
||||
inboxSubs: make(map[string][]chan<- InboxMessage),
|
||||
labelledInboxSubs: make(map[string][]chan<- InboxMessage),
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
@@ -362,14 +363,13 @@ func (e *Envoy) Dismiss() {
|
||||
close(sub)
|
||||
}
|
||||
|
||||
for _, subs := range e.inboxSubs {
|
||||
for _, sub := range subs {
|
||||
for _, sub := range e.inboxSubs {
|
||||
close(sub)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -388,11 +388,12 @@ func (e *Envoy) SubscribeInbox(labels []string) <-chan InboxMessage {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
ch := make(chan InboxMessage)
|
||||
e.inboxSubs = append(e.inboxSubs, ch)
|
||||
for _, label := range labels {
|
||||
if _, ok := e.inboxSubs[label]; !ok {
|
||||
e.inboxSubs[label] = make([]chan<- InboxMessage, 0)
|
||||
if _, ok := e.labelledInboxSubs[label]; !ok {
|
||||
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
|
||||
}
|
||||
@@ -448,7 +449,7 @@ func (e *Envoy) routeInbox() {
|
||||
}
|
||||
|
||||
e.mu.RLock()
|
||||
subs, ok := e.inboxSubs[string(label)]
|
||||
subs, ok := e.labelledInboxSubs[string(label)]
|
||||
e.mu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
|
||||
@@ -22,11 +22,21 @@ func TestEnvoy_Dismiss(t *testing.T) {
|
||||
}
|
||||
|
||||
envoy := newEnvoy(ctx, url, terminate, nil, nil, nil, nil)
|
||||
|
||||
eventSub := envoy.SubscribeEvents()
|
||||
inboxSub := envoy.SubscribeInbox([]string{"A", "B"})
|
||||
|
||||
envoy.Dismiss()
|
||||
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
assert.True(t, terminated)
|
||||
|
||||
_, ok := <-eventSub
|
||||
assert.False(t, ok)
|
||||
|
||||
_, ok = <-inboxSub
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestEnvoy_Send(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user