mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-18 05:42:20 +01:00
fix atomicity of subscription ids.
This commit is contained in:
parent
b7ec430166
commit
bc783a3a24
9
relay.go
9
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),
|
||||
}
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user