Files
local-server/cmd/dkl-local-server/mutex.go

29 lines
404 B
Go
Raw Normal View History

2026-04-21 08:41:53 +02:00
package main
import "sync"
var (
opMutexes = map[string]*sync.Mutex{}
opsLock sync.Mutex
)
func opMutex[T any](key string, op func() (T, error)) (T, error) {
lock := func() *sync.Mutex {
opsLock.Lock()
defer opsLock.Unlock()
lock, ok := opMutexes[key]
if !ok {
lock = new(sync.Mutex)
opMutexes[key] = lock
}
return lock
}()
lock.Lock()
defer lock.Unlock()
return op()
}