mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-24 15:52:25 +02: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 {
|
func (r *Relay) prepareSubscription(id string) *Subscription {
|
||||||
sub := &Subscription{
|
sub := &Subscription{
|
||||||
|
Relay: r,
|
||||||
conn: r.Connection,
|
conn: r.Connection,
|
||||||
id: id,
|
id: id,
|
||||||
Events: make(chan Event),
|
Events: make(chan Event),
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
package nostr
|
package nostr
|
||||||
|
|
||||||
import "sync"
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type Subscription struct {
|
type Subscription struct {
|
||||||
id string
|
id string
|
||||||
conn *Connection
|
conn *Connection
|
||||||
|
mutex sync.Mutex
|
||||||
|
|
||||||
|
Relay *Relay
|
||||||
Filters Filters
|
Filters Filters
|
||||||
Events chan Event
|
Events chan Event
|
||||||
EndOfStoredEvents chan struct{}
|
EndOfStoredEvents chan struct{}
|
||||||
@ -19,13 +23,15 @@ type EventMessage struct {
|
|||||||
Relay string
|
Relay string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sub Subscription) Unsub() {
|
func (sub *Subscription) Unsub() {
|
||||||
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id})
|
sub.mutex.Lock()
|
||||||
|
defer sub.mutex.Unlock()
|
||||||
|
|
||||||
sub.stopped = true
|
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id})
|
||||||
if sub.Events != nil {
|
if sub.stopped == false && sub.Events != nil {
|
||||||
close(sub.Events)
|
close(sub.Events)
|
||||||
}
|
}
|
||||||
|
sub.stopped = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sub *Subscription) Sub(filters Filters) {
|
func (sub *Subscription) Sub(filters Filters) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user