mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-18 05:42:20 +01:00
prevent sending on closed channel for subscription.
This commit is contained in:
parent
67d8f26d8a
commit
2d01aa8630
1
relay.go
1
relay.go
@ -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),
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user