use xsync maps for pool relays to prevent concurrent access panics.

This commit is contained in:
fiatjaf
2023-11-16 14:51:33 -03:00
parent 9287b9fb5e
commit 307df51b9a
+4 -4
View File
@@ -10,7 +10,7 @@ import (
) )
type SimplePool struct { type SimplePool struct {
Relays map[string]*Relay Relays *xsync.MapOf[string, *Relay]
Context context.Context Context context.Context
cancel context.CancelFunc cancel context.CancelFunc
@@ -25,7 +25,7 @@ func NewSimplePool(ctx context.Context) *SimplePool {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
return &SimplePool{ return &SimplePool{
Relays: make(map[string]*Relay), Relays: xsync.NewMapOf[*Relay](),
Context: ctx, Context: ctx,
cancel: cancel, cancel: cancel,
@@ -37,7 +37,7 @@ func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
defer namedLock(url)() defer namedLock(url)()
relay, ok := pool.Relays[nm] relay, ok := pool.Relays.Load(nm)
if ok && relay.IsConnected() { if ok && relay.IsConnected() {
// already connected, unlock and return // already connected, unlock and return
return relay, nil return relay, nil
@@ -50,7 +50,7 @@ func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
return nil, fmt.Errorf("failed to connect: %w", err) return nil, fmt.Errorf("failed to connect: %w", err)
} }
pool.Relays[nm] = relay pool.Relays.Store(nm, relay)
return relay, nil return relay, nil
} }
} }