chore: make relay cleanup more clear

This commit is contained in:
Roland Bewick
2025-11-15 17:37:55 +07:00
committed by fiatjaf_
parent 78da155663
commit 18369d4695

View File

@@ -167,16 +167,19 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
// queue all write operations here so we don't do mutex spaghetti // queue all write operations here so we don't do mutex spaghetti
go func() { go func() {
defer func() {
ticker.Stop()
r.Connection = nil
for _, sub := range r.Subscriptions.Range {
sub.unsub(fmt.Errorf("relay connection closed: %w / %w", context.Cause(r.connectionContext), r.ConnectionError))
}
}()
pingAttempt := 0 pingAttempt := 0
for { for {
select { select {
case <-r.connectionContext.Done(): case <-r.connectionContext.Done():
ticker.Stop()
r.Connection = nil
for _, sub := range r.Subscriptions.Range {
sub.unsub(fmt.Errorf("relay connection closed: %w / %w", context.Cause(r.connectionContext), r.ConnectionError))
}
return return
case <-ticker.C: case <-ticker.C:
@@ -192,6 +195,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
if err != nil { if err != nil {
debugLogf("{%s} failed to close relay: %v", r.URL, err) debugLogf("{%s} failed to close relay: %v", r.URL, err)
} }
return
} }
continue continue
} }