fix atomicity of subscription ids.

This commit is contained in:
fiatjaf 2023-06-09 11:01:42 -03:00
parent b7ec430166
commit bc783a3a24
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 6 additions and 7 deletions

View File

@ -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),
}

View File

@ -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.