From 69ccfbaa08d6eb154e4974813c4e044ed97ae252 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 16 Nov 2022 10:07:15 -0300 Subject: [PATCH] protect against faulty relays that send more than one EOSE halting us using sync.Once{} to only emit to the EndOfStoredEvents channel once (it has capacity 1 so anything over that would halt). --- relay.go | 4 +++- subscription.go | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/relay.go b/relay.go index f4c069b..925bc3c 100644 --- a/relay.go +++ b/relay.go @@ -140,7 +140,9 @@ func (r *Relay) Connect() error { var channel string json.Unmarshal(jsonMessage[1], &channel) if subscription, ok := r.subscriptions.Load(channel); ok { - subscription.EndOfStoredEvents <- struct{}{} + subscription.emitEose.Do(func() { + subscription.EndOfStoredEvents <- struct{}{} + }) } case "OK": if len(jsonMessage) < 3 { diff --git a/subscription.go b/subscription.go index 3c0a0f4..b2cb186 100644 --- a/subscription.go +++ b/subscription.go @@ -1,5 +1,7 @@ package nostr +import "sync" + type Subscription struct { id string conn *Connection @@ -8,7 +10,8 @@ type Subscription struct { Events chan Event EndOfStoredEvents chan struct{} - stopped bool + stopped bool + emitEose sync.Once } type EventMessage struct {