Skip to content

Commit

Permalink
subscription must be deemed closed when receiving a CLOSED.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 15, 2024
1 parent f9271a6 commit 9e0a86d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
}
case *ClosedEnvelope:
if subscription, ok := r.Subscriptions.Load(subIdToSerial(env.SubscriptionID)); ok {
subscription.dispatchClosed(env.Reason)
subscription.handleClosed(env.Reason)
}
case *CountEnvelope:
if subscription, ok := r.Subscriptions.Load(subIdToSerial(env.SubscriptionID)); ok && env.Count != nil && subscription.countResult != nil {
Expand Down
13 changes: 6 additions & 7 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type Subscription struct {
match func(*Event) bool // this will be either Filters.Match or Filters.MatchIgnoringTimestampConstraints
live atomic.Bool
eosed atomic.Bool
closed atomic.Bool
cancel context.CancelFunc

// this keeps track of the events we've received before the EOSE that we must dispatch before
Expand Down Expand Up @@ -108,12 +107,12 @@ func (sub *Subscription) dispatchEose() {
}
}

func (sub *Subscription) dispatchClosed(reason string) {
if sub.closed.CompareAndSwap(false, true) {
go func() {
sub.ClosedReason <- reason
}()
}
func (sub *Subscription) handleClosed(reason string) {
go func() {
sub.ClosedReason <- reason
}()
sub.live.Store(false) // set this so we don't send an unnecessary CLOSE to the relay
sub.Unsub()
}

// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
Expand Down

0 comments on commit 9e0a86d

Please sign in to comment.