use a named lock instead of a single per-pool mutex.

This commit is contained in:
fiatjaf
2023-08-06 19:54:30 -03:00
parent 2e9cdc8255
commit b522d24c30
2 changed files with 13 additions and 3 deletions

View File

@@ -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