subscription ids as sequential numbers and sub.GetID()

This commit is contained in:
fiatjaf
2023-03-17 16:43:48 -03:00
parent cef5892bce
commit fdc99d61b6
2 changed files with 16 additions and 14 deletions

View File

@@ -2,8 +2,6 @@ package nostr
import ( import (
"context" "context"
"crypto/rand"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@@ -23,6 +21,8 @@ const (
PublishStatusSucceeded Status = 1 PublishStatusSucceeded Status = 1
) )
var subscriptionIdCounter = 0
func (s Status) String() string { func (s Status) String() string {
switch s { switch s {
case PublishStatusSent: case PublishStatusSent:
@@ -41,7 +41,7 @@ type Relay struct {
RequestHeader http.Header // e.g. for origin header RequestHeader http.Header // e.g. for origin header
Connection *Connection Connection *Connection
subscriptions s.MapOf[string, *Subscription] subscriptions s.MapOf[int, *Subscription]
Challenges chan string // NIP-42 Challenges Challenges chan string // NIP-42 Challenges
Notices chan string Notices chan string
@@ -149,7 +149,7 @@ func (r *Relay) Connect(ctx context.Context) error {
continue continue
} }
var channel string var channel int
json.Unmarshal(jsonMessage[1], &channel) json.Unmarshal(jsonMessage[1], &channel)
if subscription, ok := r.subscriptions.Load(channel); ok { if subscription, ok := r.subscriptions.Load(channel); ok {
var event Event var event Event
@@ -181,7 +181,7 @@ func (r *Relay) Connect(ctx context.Context) error {
if len(jsonMessage) < 2 { if len(jsonMessage) < 2 {
continue continue
} }
var channel string var channel int
json.Unmarshal(jsonMessage[1], &channel) json.Unmarshal(jsonMessage[1], &channel)
if subscription, ok := r.subscriptions.Load(channel); ok { if subscription, ok := r.subscriptions.Load(channel); ok {
subscription.emitEose.Do(func() { subscription.emitEose.Do(func() {
@@ -386,13 +386,9 @@ func (r *Relay) QuerySync(ctx context.Context, filter Filter) []*Event {
} }
func (r *Relay) PrepareSubscription() *Subscription { func (r *Relay) PrepareSubscription() *Subscription {
random := make([]byte, 7) id := subscriptionIdCounter
rand.Read(random) subscriptionIdCounter++
id := hex.EncodeToString(random)
return r.prepareSubscription(id)
}
func (r *Relay) prepareSubscription(id string) *Subscription {
sub := &Subscription{ sub := &Subscription{
Relay: r, Relay: r,
conn: r.Connection, conn: r.Connection,

View File

@@ -2,11 +2,12 @@ package nostr
import ( import (
"context" "context"
"strconv"
"sync" "sync"
) )
type Subscription struct { type Subscription struct {
id string id int
conn *Connection conn *Connection
mutex sync.Mutex mutex sync.Mutex
@@ -25,13 +26,18 @@ type EventMessage struct {
Relay string Relay 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 strconv.Itoa(sub.id)
}
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01. // Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
// Unsub() also closes the channel sub.Events. // Unsub() also closes the channel sub.Events.
func (sub *Subscription) Unsub() { func (sub *Subscription) Unsub() {
sub.mutex.Lock() sub.mutex.Lock()
defer sub.mutex.Unlock() defer sub.mutex.Unlock()
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id}) sub.conn.WriteJSON([]interface{}{"CLOSE", strconv.Itoa(sub.id)})
if sub.stopped == false && sub.Events != nil { if sub.stopped == false && sub.Events != nil {
close(sub.Events) close(sub.Events)
} }
@@ -50,7 +56,7 @@ func (sub *Subscription) Fire(ctx context.Context) {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
sub.Context = ctx sub.Context = ctx
message := []interface{}{"REQ", sub.id} message := []interface{}{"REQ", strconv.Itoa(sub.id)}
for _, filter := range sub.Filters { for _, filter := range sub.Filters {
message = append(message, filter) message = append(message, filter)
} }