From 9a515ce4d1a06123a5f9cc57b1de7ee7274da89d Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Tue, 18 Aug 2026 13:19:06 +0900 Subject: [PATCH] nostrfs: reuse pooled relay connections when publishing notes instead of leaking a new connection on every publish. --- nostrfs_cgo/root.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/nostrfs_cgo/root.go b/nostrfs_cgo/root.go index 04eb393..71238a6 100644 --- a/nostrfs_cgo/root.go +++ b/nostrfs_cgo/root.go @@ -996,13 +996,18 @@ func (r *NostrRoot) publishNote(path string) { } for _, url := range relays { - connectCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - relay, err := nostr.RelayConnect(connectCtx, url, r.sys.Pool.RelayOptions) - cancel() - if err != nil { - continue + nm := nostr.NormalizeURL(url) + relay, ok := r.sys.Pool.Relays.Load(nm) + if !ok || relay == nil || !relay.IsConnected() { + connectCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + var err error + relay, err = nostr.RelayConnect(connectCtx, url, r.sys.Pool.RelayOptions) + cancel() + if err != nil { + continue + } + r.sys.Pool.Relays.Store(nm, relay) } - r.sys.Pool.Relays.Store(nostr.NormalizeURL(url), relay) relay.Publish(ctx, *evt) }