From 6f1c60f66c0274e4e77681af55e6fd9cb76e6399 Mon Sep 17 00:00:00 2001 From: stanstacks Date: Thu, 22 Dec 2022 18:48:41 +0800 Subject: [PATCH 1/3] problem: can't unsub --- README.md | 6 +++--- relaypool.go | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 437b2f3..08c5c4f 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ for notice := range pool.Notices { ### Listening for events ```go -subId, events := pool.Sub(nostr.Filters{ +subId, events, unsub := pool.Sub(nostr.Filters{ { Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"}, Kinds: []int{nostr.KindTextNote}, // or {1} @@ -38,7 +38,7 @@ go func() { }() time.Sleep(5 * time.Second) -sub.Unsub() +close(unsub) ``` ### Publishing an event @@ -84,4 +84,4 @@ fmt.Println("pk:", nostr.GetPublicKey(sk)) ``` go run example/example.go -``` +``` \ No newline at end of file diff --git a/relaypool.go b/relaypool.go index ddf557c..57033e5 100644 --- a/relaypool.go +++ b/relaypool.go @@ -124,7 +124,7 @@ func (r *RelayPool) Remove(url string) { } } -func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage) { +func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage, chan struct{}) { random := make([]byte, 7) rand.Read(random) id := hex.EncodeToString(random) @@ -132,6 +132,7 @@ func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage) { r.subscriptions.Store(id, filters) eventStream := make(chan EventMessage) r.eventStreams.Store(id, eventStream) + unsub := make(chan struct{}) r.Relays.Range(func(_ string, relay *Relay) bool { sub := relay.prepareSubscription(id) @@ -143,10 +144,17 @@ func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage) { } }(sub) + go func() { + select { + case <-unsub: + sub.Unsub() + } + }() + return true }) - return id, eventStream + return id, eventStream, unsub } func Unique(all chan EventMessage) chan Event { From c94ae093c150e7889f2b8020c09c14ebacf8e3a0 Mon Sep 17 00:00:00 2001 From: stanstacks Date: Fri, 23 Dec 2022 10:03:40 +0800 Subject: [PATCH 2/3] problem: close channel to unsub is confusing --- README.md | 2 +- relaypool.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 08c5c4f..8104aaf 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ go func() { }() time.Sleep(5 * time.Second) -close(unsub) +unsub() ``` ### Publishing an event diff --git a/relaypool.go b/relaypool.go index 57033e5..5c15b2f 100644 --- a/relaypool.go +++ b/relaypool.go @@ -124,7 +124,7 @@ func (r *RelayPool) Remove(url string) { } } -func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage, chan struct{}) { +func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage, func()) { random := make([]byte, 7) rand.Read(random) id := hex.EncodeToString(random) @@ -154,7 +154,7 @@ func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage, chan struct return true }) - return id, eventStream, unsub + return id, eventStream, func() { close(unsub) } } func Unique(all chan EventMessage) chan Event { From 6452f7de093745347db461fca923c5bdeae9b829 Mon Sep 17 00:00:00 2001 From: stanstacks Date: Fri, 23 Dec 2022 10:12:22 +0800 Subject: [PATCH 3/3] problem: example is broken --- README.md | 4 ++-- example/example.go | 18 ++++++++++-------- relaypool.go | 5 ++++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8104aaf..fb2162c 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ for notice := range pool.Notices { ### Listening for events ```go -subId, events, unsub := pool.Sub(nostr.Filters{ +subId, events, unsubscribe := pool.Sub(nostr.Filters{ { Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"}, Kinds: []int{nostr.KindTextNote}, // or {1} @@ -38,7 +38,7 @@ go func() { }() time.Sleep(5 * time.Second) -unsub() +unsubscribe() ``` ### Publishing an event diff --git a/example/example.go b/example/example.go index 846bdfd..502d917 100644 --- a/example/example.go +++ b/example/example.go @@ -8,7 +8,7 @@ import ( ) // some nostr relay in the wild -var relayURL = "wss://nostr-relay.wlvs.space" +var relayURL = "wss://nostr.688.org" func main() { // create key pair @@ -26,13 +26,15 @@ func main() { pool.SecretKey = &secretKey // add a nostr relay to our pool - err = pool.Add(relayURL, nostr.SimplePolicy{Read: true, Write: true}) - if err != nil { - fmt.Printf("error calling Add(): %s\n", err.Error()) - } + errchan := pool.Add(relayURL, nostr.SimplePolicy{Read: true, Write: true}) + go func() { + for err := range errchan { + fmt.Println(err.Error()) + } + }() // subscribe to relays in our pool, with filtering - sub := pool.Sub(nostr.Filters{ + _, events, unsubscribe := pool.Sub(nostr.Filters{ { Authors: []string{publicKey}, Kinds: []int{nostr.KindTextNote}, @@ -41,7 +43,7 @@ func main() { // listen for events from our subscriptions go func() { - for event := range sub.UniqueEvents { + for event := range nostr.Unique(events) { fmt.Printf("Received Event: %+v\n\n", event) } }() @@ -69,7 +71,7 @@ func main() { // after 20 seconds, unsubscribe from our pool and terminate program time.Sleep(20 * time.Second) fmt.Println("unsubscribing from nostr subscription") - sub.Unsub() + unsubscribe() } // handle events from out publish events diff --git a/relaypool.go b/relaypool.go index 5c15b2f..983e1c9 100644 --- a/relaypool.go +++ b/relaypool.go @@ -124,7 +124,10 @@ func (r *RelayPool) Remove(url string) { } } -func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage, func()) { +//Sub subscribes to events matching the passed filters and returns the subscription ID, +//a channel which you should pass into Unique to get unique events, and a function which +//you should call to clean up and close your subscription so that the relay doesn't block you. +func (r *RelayPool) Sub(filters Filters) (subID string, events chan EventMessage, unsubscribe func()) { random := make([]byte, 7) rand.Read(random) id := hex.EncodeToString(random)