replace generic-sync-map with xsync.

This commit is contained in:
fiatjaf 2023-05-30 13:52:14 -03:00
parent ad0f73fa9c
commit 39f09e6bb9
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
4 changed files with 17 additions and 12 deletions

2
go.mod
View File

@ -3,12 +3,12 @@ module github.com/nbd-wtf/go-nostr
go 1.18
require (
github.com/SaveTheRbtz/generic-sync-map-go v0.0.0-20220414055132-a37292614db8
github.com/btcsuite/btcd/btcec/v2 v2.2.0
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/gobwas/httphead v0.1.0
github.com/gobwas/ws v1.2.0
github.com/mailru/easyjson v0.7.7
github.com/puzpuzpuz/xsync v1.5.2
github.com/tidwall/gjson v1.14.4
github.com/tyler-smith/go-bip32 v1.0.0
github.com/tyler-smith/go-bip39 v1.1.0

4
go.sum
View File

@ -2,8 +2,6 @@ github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e h1:ahyvB3q25Yn
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:kGUqhHd//musdITWjFvNTHn90WG9bMLBEPQZ17Cmlpw=
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec h1:1Qb69mGp/UtRPn422BH4/Y4Q3SLUrD9KHuDkm8iodFc=
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec/go.mod h1:CD8UlnlLDiqb36L110uqiP2iSflVjx9g/3U9hCI4q2U=
github.com/SaveTheRbtz/generic-sync-map-go v0.0.0-20220414055132-a37292614db8 h1:Xa6tp8DPDhdV+k23uiTC/GrAYOe4IdyJVKtob4KW3GA=
github.com/SaveTheRbtz/generic-sync-map-go v0.0.0-20220414055132-a37292614db8/go.mod h1:ihkm1viTbO/LOsgdGoFPBSvzqvx7ibvkMzYp3CgtHik=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M=
@ -78,6 +76,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/puzpuzpuz/xsync v1.5.2 h1:yRAP4wqSOZG+/4pxJ08fPTwrfL0IzE/LKQ/cw509qGY=
github.com/puzpuzpuz/xsync v1.5.2/go.mod h1:K98BYhX3k1dQ2M63t1YNVDanbwUPmBCAhNmVrrxfiGg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.1.5-0.20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=

10
pool.go
View File

@ -5,7 +5,7 @@ import (
"fmt"
"sync"
syncmap "github.com/SaveTheRbtz/generic-sync-map-go"
"github.com/puzpuzpuz/xsync"
)
type SimplePool struct {
@ -58,7 +58,7 @@ func (pool *SimplePool) SubMany(
filters Filters,
) chan *Event {
uniqueEvents := make(chan *Event)
seenAlready := syncmap.MapOf[string, struct{}]{}
seenAlready := xsync.NewMapOf[bool]()
for _, url := range urls {
go func(nm string) {
@ -74,7 +74,7 @@ func (pool *SimplePool) SubMany(
for evt := range sub.Events {
// dispatch unique events to client
if _, ok := seenAlready.LoadOrStore(evt.ID, struct{}{}); !ok {
if _, ok := seenAlready.LoadOrStore(evt.ID, true); !ok {
uniqueEvents <- evt
}
}
@ -93,7 +93,7 @@ func (pool *SimplePool) SubManyEose(
ctx, cancel := context.WithCancel(ctx)
uniqueEvents := make(chan *Event)
seenAlready := syncmap.MapOf[string, struct{}]{}
seenAlready := xsync.NewMapOf[bool]()
wg := sync.WaitGroup{}
wg.Add(len(urls))
@ -129,7 +129,7 @@ func (pool *SimplePool) SubManyEose(
}
// dispatch unique events to client
if _, ok := seenAlready.LoadOrStore(evt.ID, struct{}{}); !ok {
if _, ok := seenAlready.LoadOrStore(evt.ID, true); !ok {
uniqueEvents <- evt
}
}

View File

@ -7,7 +7,7 @@ import (
"sync"
"time"
s "github.com/SaveTheRbtz/generic-sync-map-go"
"github.com/puzpuzpuz/xsync"
)
type Status int
@ -38,7 +38,7 @@ type Relay struct {
RequestHeader http.Header // e.g. for origin header
Connection *Connection
subscriptions s.MapOf[string, *Subscription]
subscriptions *xsync.MapOf[string, *Subscription]
Challenges chan string // NIP-42 Challenges
Notices chan string
@ -46,7 +46,7 @@ type Relay struct {
connectionContext context.Context // will be canceled when the connection closes
connectionContextCancel context.CancelFunc
okCallbacks s.MapOf[string, func(bool, string)]
okCallbacks *xsync.MapOf[string, func(bool, string)]
mutex sync.RWMutex
// custom things that aren't often used
@ -56,7 +56,12 @@ type Relay struct {
// NewRelay returns a new relay. The relay connection will be closed when the context is canceled.
func NewRelay(ctx context.Context, url string) *Relay {
return &Relay{URL: NormalizeURL(url), connectionContext: ctx}
return &Relay{
URL: NormalizeURL(url),
connectionContext: ctx,
subscriptions: xsync.NewMapOf[*Subscription](),
okCallbacks: xsync.NewMapOf[func(bool, string)](),
}
}
// RelayConnect returns a relay object connected to url.