mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-05 12:39:36 +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
|
Relays map[string]*Relay
|
||||||
Context context.Context
|
Context context.Context
|
||||||
|
|
||||||
mutex sync.Mutex
|
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,8 +30,7 @@ func NewSimplePool(ctx context.Context) *SimplePool {
|
|||||||
func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
|
func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
|
||||||
nm := NormalizeURL(url)
|
nm := NormalizeURL(url)
|
||||||
|
|
||||||
pool.mutex.Lock()
|
defer namedLock(url)()
|
||||||
defer pool.mutex.Unlock()
|
|
||||||
|
|
||||||
relay, ok := pool.Relays[nm]
|
relay, ok := pool.Relays[nm]
|
||||||
if ok && relay.IsConnected() {
|
if ok && relay.IsConnected() {
|
||||||
|
12
utils.go
12
utils.go
@ -3,10 +3,22 @@ package nostr
|
|||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/dgraph-io/ristretto/z"
|
||||||
"golang.org/x/exp/constraints"
|
"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 {
|
func similar[E constraints.Ordered](as, bs []E) bool {
|
||||||
if len(as) != len(bs) {
|
if len(as) != len(bs) {
|
||||||
return false
|
return false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user