context cancelation for relay connections and subscriptions.
This commit is contained in:
@@ -14,6 +14,7 @@ type Subscription struct {
|
||||
Filters Filters
|
||||
Events chan *Event
|
||||
EndOfStoredEvents chan struct{}
|
||||
Context context.Context
|
||||
|
||||
stopped bool
|
||||
emitEose sync.Once
|
||||
@@ -46,6 +47,9 @@ func (sub *Subscription) Sub(ctx context.Context, filters Filters) {
|
||||
// Fire sends the "REQ" command to the relay.
|
||||
// When ctx is cancelled, sub.Unsub() is called, closing the subscription.
|
||||
func (sub *Subscription) Fire(ctx context.Context) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
sub.Context = ctx
|
||||
|
||||
message := []interface{}{"REQ", sub.id}
|
||||
for _, filter := range sub.Filters {
|
||||
message = append(message, filter)
|
||||
@@ -58,4 +62,16 @@ func (sub *Subscription) Fire(ctx context.Context) {
|
||||
<-ctx.Done()
|
||||
sub.Unsub()
|
||||
}()
|
||||
|
||||
// or when the relay connection is closed
|
||||
go func() {
|
||||
<-sub.Relay.ConnectionContext.Done()
|
||||
|
||||
// this will close the Events channel,
|
||||
// which can be used by an external reader to learn the subscription has stopped
|
||||
sub.Unsub()
|
||||
|
||||
// we also cancel the context
|
||||
cancel()
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user