fix inbox sub close panic on envoy
This commit is contained in:
+28
-27
@@ -69,15 +69,16 @@ type Embassy struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Envoy struct {
|
type Envoy struct {
|
||||||
url string
|
url string
|
||||||
connected bool
|
connected bool
|
||||||
terminate func()
|
terminate func()
|
||||||
queue chan []byte
|
queue chan []byte
|
||||||
send func(data []byte) error
|
send func(data []byte) error
|
||||||
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
|
||||||
@@ -312,15 +313,15 @@ func newEnvoy(
|
|||||||
ctx, cancel := context.WithCancel(component.MustExtend(ctx, "envoy"))
|
ctx, cancel := context.WithCancel(component.MustExtend(ctx, "envoy"))
|
||||||
|
|
||||||
e := &Envoy{
|
e := &Envoy{
|
||||||
url: url,
|
url: url,
|
||||||
terminate: terminate,
|
terminate: terminate,
|
||||||
queue: make(chan []byte),
|
queue: make(chan []byte),
|
||||||
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,
|
||||||
}
|
}
|
||||||
|
|
||||||
if handler != nil {
|
if handler != nil {
|
||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user