added sub.mutex handling in the relay Connect() function (#37)

This commit is contained in:
barkyq
2023-01-15 07:19:00 -05:00
committed by GitHub
parent 635c1b0132
commit a37ffacc74
3 changed files with 25 additions and 8 deletions

View File

@@ -24,6 +24,8 @@ type EventMessage struct {
Relay string
}
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01
// Unsub() also closes the channel sub.Events
func (sub *Subscription) Unsub() {
sub.mutex.Lock()
defer sub.mutex.Unlock()
@@ -35,11 +37,14 @@ func (sub *Subscription) Unsub() {
sub.stopped = true
}
// Sub sets sub.Filters and then calls sub.Fire(ctx)
func (sub *Subscription) Sub(ctx context.Context, filters Filters) {
sub.Filters = filters
sub.Fire(ctx)
}
// 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) {
message := []interface{}{"REQ", sub.id}
for _, filter := range sub.Filters {