2023-12-22 12:13:44 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-05-23 17:21:19 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2023-12-22 12:13:44 +00:00
|
|
|
"git.myservermanager.com/varakh/upda/util"
|
|
|
|
"go.uber.org/zap"
|
2024-05-23 17:21:19 +00:00
|
|
|
"time"
|
2023-12-22 12:13:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type lockMemService struct {
|
|
|
|
registry *util.InMemoryLockRegistry
|
|
|
|
}
|
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
var (
|
|
|
|
errLockMemNotReleased = newServiceError(Conflict, errors.New("lock service: could not release lock"))
|
|
|
|
)
|
|
|
|
|
2023-12-22 12:13:44 +00:00
|
|
|
func newLockMemService() lockService {
|
2024-06-03 19:11:33 +00:00
|
|
|
zap.L().Info("Initializing in-memory locking service")
|
2023-12-22 12:13:44 +00:00
|
|
|
return &lockMemService{registry: util.NewInMemoryLockRegistry()}
|
|
|
|
}
|
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
// lock locks a given resource without any options (default expiration)
|
|
|
|
func (s *lockMemService) lock(ctx context.Context, resource string) (appLock, error) {
|
|
|
|
return s.lockWithOptions(ctx, resource, withAppLockOptionExpiry(0))
|
2023-12-22 12:13:44 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
// lockWithOptions locks a given resource, only TTL as option is supported
|
|
|
|
func (s *lockMemService) lockWithOptions(ctx context.Context, resource string, options ...appLockOption) (appLock, error) {
|
2023-12-22 12:13:44 +00:00
|
|
|
if resource == "" {
|
2024-05-23 17:21:19 +00:00
|
|
|
return nil, errorValidationNotBlank
|
|
|
|
}
|
|
|
|
|
|
|
|
var expiration time.Duration = 0
|
|
|
|
if options != nil {
|
|
|
|
lockOptions := &appLockOptions{}
|
|
|
|
for _, o := range options {
|
|
|
|
o.apply(lockOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lockOptions.expiry != nil {
|
|
|
|
expiration = *lockOptions.expiry
|
|
|
|
}
|
2023-12-22 12:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zap.L().Sugar().Debugf("Trying to lock '%s'", resource)
|
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
s.registry.LockWithTTL(resource, expiration)
|
2023-12-22 12:13:44 +00:00
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
zap.L().Sugar().Debugf("Locked '%s'", resource)
|
2023-12-22 12:13:44 +00:00
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
l := &inMemoryLock{
|
|
|
|
registry: s.registry,
|
|
|
|
resource: resource,
|
|
|
|
}
|
2023-12-22 12:13:44 +00:00
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
return l, nil
|
2023-12-22 12:13:44 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
var _ appLock = (*inMemoryLock)(nil)
|
|
|
|
|
|
|
|
type inMemoryLock struct {
|
|
|
|
registry *util.InMemoryLockRegistry
|
|
|
|
resource string
|
2023-12-22 12:13:44 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 17:21:19 +00:00
|
|
|
func (r inMemoryLock) unlock(ctx context.Context) error {
|
|
|
|
zap.L().Sugar().Debugf("Unlocking '%s'", r.resource)
|
|
|
|
|
|
|
|
if err := r.registry.Unlock(r.resource); err != nil {
|
|
|
|
return errLockMemNotReleased
|
|
|
|
}
|
|
|
|
return nil
|
2023-12-22 12:13:44 +00:00
|
|
|
}
|