From 0f7a3f01f2e7260c46775b4d96b69b7eae6a99ca Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 30 May 2023 14:01:07 -0300 Subject: [PATCH] use an atomic counter. --- relay.go | 6 +++--- subscription.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/relay.go b/relay.go index e00ffc3..5f6a391 100644 --- a/relay.go +++ b/relay.go @@ -18,7 +18,7 @@ const ( PublishStatusSucceeded Status = 1 ) -var subscriptionIdCounter = 0 +var subscriptionIdCounter xsync.Counter func (s Status) String() string { switch s { @@ -402,8 +402,8 @@ func (r *Relay) QuerySync(ctx context.Context, filter Filter) ([]*Event, error) } func (r *Relay) PrepareSubscription(ctx context.Context) *Subscription { - current := subscriptionIdCounter - subscriptionIdCounter++ + current := subscriptionIdCounter.Value() + subscriptionIdCounter.Inc() ctx, cancel := context.WithCancel(ctx) diff --git a/subscription.go b/subscription.go index d2b9388..3df922b 100644 --- a/subscription.go +++ b/subscription.go @@ -9,7 +9,7 @@ import ( type Subscription struct { label string - counter int + counter int64 conn *Connection mutex sync.Mutex @@ -38,7 +38,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.counter) + return sub.label + ":" + strconv.FormatInt(sub.counter, 10) } // Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.