Switch to locking package

The locking logic was added to its own package as it may need to be used
by other parts of the code.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2025-04-07 16:45:05 +00:00
parent e51f19acc8
commit 5ba53adf84
6 changed files with 131 additions and 62 deletions

46
locking/locking.go Normal file
View file

@ -0,0 +1,46 @@
package locking
import (
"fmt"
"sync"
)
var locker Locker
var lockerMux = sync.Mutex{}
func TryLock(key string) (bool, error) {
if locker == nil {
return false, fmt.Errorf("no locker is registered")
}
return locker.TryLock(key), nil
}
func Unlock(key string, remove bool) error {
if locker == nil {
return fmt.Errorf("no locker is registered")
}
locker.Unlock(key, remove)
return nil
}
func Delete(key string) error {
if locker == nil {
return fmt.Errorf("no locker is registered")
}
locker.Delete(key)
return nil
}
func RegisterLocker(lock Locker) error {
lockerMux.Lock()
defer lockerMux.Unlock()
if locker != nil {
return fmt.Errorf("locker already registered")
}
locker = lock
return nil
}