From 307df51b9a54e4df7018a9f7e5ec239352890e1f Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 16 Nov 2023 14:51:33 -0300 Subject: [PATCH] use xsync maps for pool relays to prevent concurrent access panics. --- pool.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pool.go b/pool.go index fc18f1f..e79df03 100644 --- a/pool.go +++ b/pool.go @@ -10,7 +10,7 @@ import ( ) type SimplePool struct { - Relays map[string]*Relay + Relays *xsync.MapOf[string, *Relay] Context context.Context cancel context.CancelFunc @@ -25,7 +25,7 @@ func NewSimplePool(ctx context.Context) *SimplePool { ctx, cancel := context.WithCancel(ctx) return &SimplePool{ - Relays: make(map[string]*Relay), + Relays: xsync.NewMapOf[*Relay](), Context: ctx, cancel: cancel, @@ -37,7 +37,7 @@ func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) { defer namedLock(url)() - relay, ok := pool.Relays[nm] + relay, ok := pool.Relays.Load(nm) if ok && relay.IsConnected() { // already connected, unlock and return return relay, nil @@ -50,7 +50,7 @@ func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) { return nil, fmt.Errorf("failed to connect: %w", err) } - pool.Relays[nm] = relay + pool.Relays.Store(nm, relay) return relay, nil } }