mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-15 19:31:29 +02:00
allocate subscription id only once on creation.
This commit is contained in:
parent
24343dbbef
commit
ad14daec9f
8
relay.go
8
relay.go
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@ -411,7 +412,12 @@ func (r *Relay) PrepareSubscription(ctx context.Context, filters Filters, opts .
|
|||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
switch o := opt.(type) {
|
switch o := opt.(type) {
|
||||||
case WithLabel:
|
case WithLabel:
|
||||||
sub.label = string(o)
|
buf := subIdPool.Get().([]byte)[:0]
|
||||||
|
buf = strconv.AppendInt(buf, sub.counter, 10)
|
||||||
|
buf = append(buf, ':')
|
||||||
|
buf = append(buf, string(o)...)
|
||||||
|
defer subIdPool.Put(buf)
|
||||||
|
sub.id = string(buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,14 +3,13 @@ package nostr
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Subscription struct {
|
type Subscription struct {
|
||||||
label string
|
|
||||||
counter int64
|
counter int64
|
||||||
|
id string
|
||||||
|
|
||||||
Relay *Relay
|
Relay *Relay
|
||||||
Filters Filters
|
Filters Filters
|
||||||
@ -62,17 +61,6 @@ func (_ WithLabel) IsSubscriptionOption() {}
|
|||||||
|
|
||||||
var _ SubscriptionOption = (WithLabel)("")
|
var _ SubscriptionOption = (WithLabel)("")
|
||||||
|
|
||||||
// GetID return the Nostr subscription ID as given to the Relay
|
|
||||||
// it is a concatenation of the label and a serial number.
|
|
||||||
func (sub *Subscription) GetID() string {
|
|
||||||
buf := subIdPool.Get().([]byte)
|
|
||||||
buf = strconv.AppendInt(buf, sub.counter, 10)
|
|
||||||
buf = append(buf, ':')
|
|
||||||
buf = append(buf, sub.label...)
|
|
||||||
defer subIdPool.Put(buf)
|
|
||||||
return string(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sub *Subscription) start() {
|
func (sub *Subscription) start() {
|
||||||
<-sub.Context.Done()
|
<-sub.Context.Done()
|
||||||
// the subscription ends once the context is canceled (if not already)
|
// the subscription ends once the context is canceled (if not already)
|
||||||
@ -84,6 +72,8 @@ func (sub *Subscription) start() {
|
|||||||
sub.mu.Unlock()
|
sub.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sub *Subscription) GetID() string { return sub.id }
|
||||||
|
|
||||||
func (sub *Subscription) dispatchEvent(evt *Event) {
|
func (sub *Subscription) dispatchEvent(evt *Event) {
|
||||||
added := false
|
added := false
|
||||||
if !sub.eosed.Load() {
|
if !sub.eosed.Load() {
|
||||||
@ -144,8 +134,7 @@ func (sub *Subscription) Unsub() {
|
|||||||
// Close just sends a CLOSE message. You probably want Unsub() instead.
|
// Close just sends a CLOSE message. You probably want Unsub() instead.
|
||||||
func (sub *Subscription) Close() {
|
func (sub *Subscription) Close() {
|
||||||
if sub.Relay.IsConnected() {
|
if sub.Relay.IsConnected() {
|
||||||
id := sub.GetID()
|
closeMsg := CloseEnvelope(sub.id)
|
||||||
closeMsg := CloseEnvelope(id)
|
|
||||||
closeb, _ := (&closeMsg).MarshalJSON()
|
closeb, _ := (&closeMsg).MarshalJSON()
|
||||||
<-sub.Relay.Write(closeb)
|
<-sub.Relay.Write(closeb)
|
||||||
}
|
}
|
||||||
@ -160,13 +149,11 @@ func (sub *Subscription) Sub(_ context.Context, filters Filters) {
|
|||||||
|
|
||||||
// Fire sends the "REQ" command to the relay.
|
// Fire sends the "REQ" command to the relay.
|
||||||
func (sub *Subscription) Fire() error {
|
func (sub *Subscription) Fire() error {
|
||||||
id := sub.GetID()
|
|
||||||
|
|
||||||
var reqb []byte
|
var reqb []byte
|
||||||
if sub.countResult == nil {
|
if sub.countResult == nil {
|
||||||
reqb, _ = ReqEnvelope{id, sub.Filters}.MarshalJSON()
|
reqb, _ = ReqEnvelope{sub.id, sub.Filters}.MarshalJSON()
|
||||||
} else {
|
} else {
|
||||||
reqb, _ = CountEnvelope{id, sub.Filters, nil}.MarshalJSON()
|
reqb, _ = CountEnvelope{sub.id, sub.Filters, nil}.MarshalJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
sub.live.Store(true)
|
sub.live.Store(true)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user