Skip to content

Commit b00987c

Browse files
committed
fix a bug that could cause "Helium.dispatch" to panic
1 parent 4aa9a48 commit b00987c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

discovery/helium/helium.go

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ func (h *Helium) start(ctx context.Context) {
7878

7979
func (h *Helium) dispatch(status types.ServiceStatus) {
8080
h.subs.Range(func(k, v interface{}) bool {
81+
defer func() {
82+
if err := recover(); err != nil {
83+
log.Errorf(nil, "[dispatch] dispatch %s failed, err: %v", k, err)
84+
}
85+
}()
8186
c, ok := v.(chan<- types.ServiceStatus)
8287
if !ok {
8388
log.Error("[WatchServiceStatus] failed to cast channel from map")

discovery/helium/helium_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,33 @@ func TestHelium(t *testing.T) {
4747
close(chAddr)
4848
close(chStatus)
4949
}
50+
51+
func TestPanic(t *testing.T) {
52+
chAddr := make(chan []string)
53+
54+
store := &storemocks.Store{}
55+
store.On("ServiceStatusStream", mock.Anything).Return(chAddr, nil)
56+
57+
grpcConfig := types.GRPCConfig{
58+
ServiceDiscoveryPushInterval: time.Duration(1) * time.Second,
59+
}
60+
service := New(grpcConfig, store)
61+
62+
for i := 0; i < 1000; i++ {
63+
go func() {
64+
chStatus := make(chan types.ServiceStatus)
65+
uuid := service.Subscribe(chStatus)
66+
time.Sleep(time.Second)
67+
service.Unsubscribe(uuid)
68+
close(chStatus)
69+
}()
70+
}
71+
72+
go func() {
73+
for i := 0; i < 1000; i++ {
74+
chAddr <- []string{"hhh", "hhh2"}
75+
}
76+
}()
77+
78+
time.Sleep(5 * time.Second)
79+
}

0 commit comments

Comments
 (0)