From a30f422d7dc8f371aebdcb5312aec6ecad48d91d Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 11 Jan 2024 21:29:39 -0300 Subject: [PATCH] close relay websockets cleanly. --- event.go | 6 ++++++ fetch.go | 10 +++++++++- req.go | 6 ++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/event.go b/event.go index cf1c5d1..59e6ec3 100644 --- a/event.go +++ b/event.go @@ -108,6 +108,12 @@ example: } } + defer func() { + for _, relay := range relays { + relay.Close() + } + }() + // gather the secret key sec, err := gatherSecretKeyFromArguments(c) if err != nil { diff --git a/fetch.go b/fetch.go index 86936a7..33cf46c 100644 --- a/fetch.go +++ b/fetch.go @@ -24,6 +24,15 @@ var fetch = &cli.Command{ }, ArgsUsage: "[nip19code]", Action: func(c *cli.Context) error { + pool := nostr.NewSimplePool(c.Context) + + defer func() { + pool.Relays.Range(func(_ string, relay *nostr.Relay) bool { + relay.Close() + return true + }) + }() + for code := range getStdinLinesOrFirstArgument(c) { filter := nostr.Filter{} @@ -67,7 +76,6 @@ var fetch = &cli.Command{ authorHint = v } - pool := nostr.NewSimplePool(c.Context) if authorHint != "" { relayList := sdk.FetchRelaysForPubkey(c.Context, pool, authorHint, "wss://purplepag.es", "wss://relay.damus.io", "wss://relay.noswhere.com", diff --git a/req.go b/req.go index 8819f84..eda6c08 100644 --- a/req.go +++ b/req.go @@ -135,6 +135,12 @@ example: for i, relay := range relays { relayUrls[i] = relay.URL } + + defer func() { + for _, relay := range relays { + relay.Close() + } + }() } for stdinFilter := range getStdinLinesOrBlank() {