prevent sending on closed channel for subscription.

This commit is contained in:
fiatjaf 2022-11-26 09:25:51 -03:00
parent 67d8f26d8a
commit 2d01aa8630
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 14 additions and 7 deletions

View File

@ -232,6 +232,7 @@ func (r *Relay) PrepareSubscription() *Subscription {
func (r *Relay) prepareSubscription(id string) *Subscription {
sub := &Subscription{
Relay: r,
conn: r.Connection,
id: id,
Events: make(chan Event),

View File

@ -1,11 +1,15 @@
package nostr
import "sync"
import (
"sync"
)
type Subscription struct {
id string
conn *Connection
id string
conn *Connection
mutex sync.Mutex
Relay *Relay
Filters Filters
Events chan Event
EndOfStoredEvents chan struct{}
@ -19,13 +23,15 @@ type EventMessage struct {
Relay string
}
func (sub Subscription) Unsub() {
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id})
func (sub *Subscription) Unsub() {
sub.mutex.Lock()
defer sub.mutex.Unlock()
sub.stopped = true
if sub.Events != nil {
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id})
if sub.stopped == false && sub.Events != nil {
close(sub.Events)
}
sub.stopped = true
}
func (sub *Subscription) Sub(filters Filters) {