mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-05-29 17:19:16 +02:00
use a named lock instead of a single per-pool mutex.
This commit is contained in:
parent
2e9cdc8255
commit
b522d24c30
4
pool.go
4
pool.go
@ -13,7 +13,6 @@ type SimplePool struct {
|
||||
Relays map[string]*Relay
|
||||
Context context.Context
|
||||
|
||||
mutex sync.Mutex
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
@ -31,8 +30,7 @@ func NewSimplePool(ctx context.Context) *SimplePool {
|
||||
func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
|
||||
nm := NormalizeURL(url)
|
||||
|
||||
pool.mutex.Lock()
|
||||
defer pool.mutex.Unlock()
|
||||
defer namedLock(url)()
|
||||
|
||||
relay, ok := pool.Relays[nm]
|
||||
if ok && relay.IsConnected() {
|
||||
|
12
utils.go
12
utils.go
@ -3,10 +3,22 @@ package nostr
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/dgraph-io/ristretto/z"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
const MAX_LOCKS = 50
|
||||
|
||||
var namedMutexPool = make([]sync.Mutex, MAX_LOCKS)
|
||||
|
||||
func namedLock(name string) (unlock func()) {
|
||||
idx := z.MemHashString(name) % MAX_LOCKS
|
||||
namedMutexPool[idx].Lock()
|
||||
return namedMutexPool[idx].Unlock
|
||||
}
|
||||
|
||||
func similar[E constraints.Ordered](as, bs []E) bool {
|
||||
if len(as) != len(bs) {
|
||||
return false
|
||||
|
Loading…
x
Reference in New Issue
Block a user