Skip to content

Commit 28a3f68

Browse files
authored
fix: added once wrapper to unsubscribe to prevent double close channel panic (#8)
Signed-off-by: Andres Morey <[email protected]>
1 parent 4c41dd8 commit 28a3f68

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

dispatcher.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,17 @@ type DispatchHandler func(ctx context.Context, conn *grpc.ClientConn)
6464

6565
// Represents interest in pod ips that are part of a Kubernetes service
6666
type Subscription struct {
67-
serverCh chan server
68-
cleanup func()
67+
serverCh chan server
68+
cleanup func()
69+
unsubscribeOnce sync.Once
6970
}
7071

7172
// Ends subscription
7273
func (sub *Subscription) Unsubscribe() {
73-
close(sub.serverCh)
74-
sub.cleanup()
74+
sub.unsubscribeOnce.Do(func() {
75+
close(sub.serverCh)
76+
sub.cleanup()
77+
})
7578
}
7679

7780
// A Dispatcher is a utility that facilitates sending queries to multiple grpc servers

0 commit comments

Comments
 (0)