From dd59887a3d8aaf178e4df05c4f68e9716fec7b97 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 5 Jan 2024 19:52:05 +0700 Subject: [PATCH] fix: listen to relay context done message --- .env.example | 1 + service.go | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index e0e7c094..5aa2ed4f 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,7 @@ DATABASE_URI=file:nwc.db NOSTR_PRIVKEY= COOKIE_SECRET=secretsecret RELAY=wss://relay.getalby.com/v1 +#RELAY=ws://localhost:7447/v1 PUBLIC_RELAY= PORT=8080 diff --git a/service.go b/service.go index 5efa4b1f..8707bdf4 100644 --- a/service.go +++ b/service.go @@ -128,17 +128,18 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti } }() - <-ctx.Done() - if sub.Relay.ConnectionError != nil { - svc.Logger.Errorf("Relay error %v", ctx.Err()) + select { + case <-sub.Relay.Context().Done(): + svc.Logger.Errorf("Relay error %v", sub.Relay.ConnectionError) return sub.Relay.ConnectionError + case <-ctx.Done(): + if ctx.Err() != context.Canceled { + svc.Logger.Errorf("Subscription error %v", ctx.Err()) + return ctx.Err() + } + svc.Logger.Info("Exiting subscription.") + return nil } - if ctx.Err() != context.Canceled { - svc.Logger.Errorf("Subscription error %v", ctx.Err()) - return ctx.Err() - } - svc.Logger.Info("Exiting subscription.") - return nil } func (svc *Service) HandleEvent(ctx context.Context, event *nostr.Event) (result *nostr.Event, err error) {