diff --git a/relay.go b/relay.go index 5143f33..1c3e13a 100644 --- a/relay.go +++ b/relay.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "sync" + "sync/atomic" "time" "github.com/puzpuzpuz/xsync" @@ -18,7 +19,7 @@ const ( PublishStatusSucceeded Status = 1 ) -var subscriptionIdCounter xsync.Counter +var subscriptionIdCounter atomic.Int32 func (s Status) String() string { switch s { @@ -403,9 +404,7 @@ func (r *Relay) QuerySync(ctx context.Context, filter Filter) ([]*Event, error) } func (r *Relay) PrepareSubscription(ctx context.Context) *Subscription { - current := subscriptionIdCounter.Value() - subscriptionIdCounter.Inc() - + current := subscriptionIdCounter.Add(1) ctx, cancel := context.WithCancel(ctx) return &Subscription{ @@ -413,7 +412,7 @@ func (r *Relay) PrepareSubscription(ctx context.Context) *Subscription { Context: ctx, cancel: cancel, conn: r.Connection, - counter: current, + counter: int(current), Events: make(chan *Event), EndOfStoredEvents: make(chan struct{}, 1), } diff --git a/subscription.go b/subscription.go index 3c8db63..564569e 100644 --- a/subscription.go +++ b/subscription.go @@ -9,7 +9,7 @@ import ( type Subscription struct { label string - counter int64 + counter int 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.FormatInt(sub.counter, 10) + return sub.label + ":" + strconv.Itoa(sub.counter) } // Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.