From bb1138a2fa2dad5d329bf149a280814f9e9bd8af Mon Sep 17 00:00:00 2001 From: Stan Stacker <120361519+stanstacks@users.noreply.github.com> Date: Sun, 25 Dec 2022 04:58:44 +0000 Subject: [PATCH] problem: panic if calling unsub more than once problem: panic if calling unsub more than once It's a bit dangerous if calling unsub more than once causes a fatal panic. --- relaypool.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/relaypool.go b/relaypool.go index 983e1c9..5455d54 100644 --- a/relaypool.go +++ b/relaypool.go @@ -157,7 +157,15 @@ func (r *RelayPool) Sub(filters Filters) (subID string, events chan EventMessage return true }) - return id, eventStream, func() { close(unsub) } + return id, eventStream, func() { gracefulClose(unsub) } +} + +func gracefulClose(c chan struct{}) { + select { + case <-c: + default: + close(c) + } } func Unique(all chan EventMessage) chan Event {