fixing subscription labels and ids.

This commit is contained in:
fiatjaf 2023-03-18 15:09:49 -03:00
parent 3f66c60b5f
commit 85fc74fd22
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 10 additions and 11 deletions

View File

@ -385,19 +385,16 @@ func (r *Relay) QuerySync(ctx context.Context, filter Filter) []*Event {
}
func (r *Relay) PrepareSubscription() *Subscription {
id := subscriptionIdCounter
current := subscriptionIdCounter
subscriptionIdCounter++
sub := &Subscription{
return &Subscription{
Relay: r,
conn: r.Connection,
id: id,
counter: current,
Events: make(chan *Event),
EndOfStoredEvents: make(chan struct{}, 1),
}
r.subscriptions.Store(sub.GetID(), sub)
return sub
}
func (r *Relay) Close() error {

View File

@ -7,10 +7,10 @@ import (
)
type Subscription struct {
label string
id int
conn *Connection
mutex sync.Mutex
label string
counter int
conn *Connection
mutex sync.Mutex
Relay *Relay
Filters Filters
@ -35,7 +35,7 @@ func (sub *Subscription) SetLabel(label string) {
// GetID return the Nostr subscription ID as given to the relay, it will be a sequential number, stringified.
func (sub *Subscription) GetID() string {
return sub.label + ":" + strconv.Itoa(sub.id)
return sub.label + ":" + strconv.Itoa(sub.counter)
}
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
@ -60,6 +60,8 @@ func (sub *Subscription) Sub(ctx context.Context, filters Filters) {
// 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) error {
sub.Relay.subscriptions.Store(sub.GetID(), sub)
ctx, cancel := context.WithCancel(ctx)
sub.Context = ctx