mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-08-31 17:58:15 +02:00
simplify subscription closing.
This commit is contained in:
17
relay.go
17
relay.go
@@ -482,15 +482,14 @@ func (r *Relay) PrepareSubscription(ctx context.Context, filters Filters, opts .
|
|||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
sub := &Subscription{
|
sub := &Subscription{
|
||||||
Relay: r,
|
Relay: r,
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
counter: int(current),
|
counter: int(current),
|
||||||
Events: make(chan *Event),
|
Events: make(chan *Event),
|
||||||
events: make(chan *Event),
|
events: make(chan *Event),
|
||||||
EndOfStoredEvents: make(chan struct{}),
|
EndOfStoredEvents: make(chan struct{}),
|
||||||
Filters: filters,
|
Filters: filters,
|
||||||
closeEventsChannel: make(chan struct{}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
@@ -29,10 +29,9 @@ type Subscription struct {
|
|||||||
// Context will be .Done() when the subscription ends
|
// Context will be .Done() when the subscription ends
|
||||||
Context context.Context
|
Context context.Context
|
||||||
|
|
||||||
live atomic.Bool
|
live atomic.Bool
|
||||||
eosed atomic.Bool
|
eosed atomic.Bool
|
||||||
closeEventsChannel chan struct{}
|
cancel context.CancelFunc
|
||||||
cancel context.CancelFunc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventMessage struct {
|
type EventMessage struct {
|
||||||
@@ -78,14 +77,14 @@ func (sub *Subscription) start() {
|
|||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}()
|
}()
|
||||||
case <-sub.Context.Done():
|
case <-sub.Context.Done():
|
||||||
// the subscription ends once the context is canceled
|
// the subscription ends once the context is canceled (if not already)
|
||||||
sub.Unsub()
|
sub.Unsub() // this will set sub.live to false
|
||||||
return
|
|
||||||
case <-sub.closeEventsChannel:
|
// do this so we don't have the possibility of closing the Events channel and then trying to send to it
|
||||||
// this is called only once on .Unsub() and closes the .Events channel
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
close(sub.Events)
|
close(sub.Events)
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,17 +93,16 @@ func (sub *Subscription) start() {
|
|||||||
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
|
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
|
||||||
// Unsub() also closes the channel sub.Events and makes a new one.
|
// Unsub() also closes the channel sub.Events and makes a new one.
|
||||||
func (sub *Subscription) Unsub() {
|
func (sub *Subscription) Unsub() {
|
||||||
|
// cancel the context (if it's not canceled already)
|
||||||
sub.cancel()
|
sub.cancel()
|
||||||
|
|
||||||
// naïve sync.Once implementation:
|
// mark subscription as closed and send a CLOSE to the relay (naïve sync.Once implementation)
|
||||||
if sub.live.CompareAndSwap(true, false) {
|
if sub.live.CompareAndSwap(true, false) {
|
||||||
go sub.Close()
|
sub.Close()
|
||||||
id := sub.GetID()
|
|
||||||
sub.Relay.Subscriptions.Delete(id)
|
|
||||||
|
|
||||||
// do this so we don't have the possibility of closing the Events channel and then trying to send to it
|
|
||||||
close(sub.closeEventsChannel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove subscription from our map
|
||||||
|
sub.Relay.Subscriptions.Delete(sub.GetID())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close just sends a CLOSE message. You probably want Unsub() instead.
|
// Close just sends a CLOSE message. You probably want Unsub() instead.
|
||||||
|
Reference in New Issue
Block a user