1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-23 08:16:36 +00:00

lock: cleanup

This commit is contained in:
Michael Eischer 2023-06-17 19:13:05 +02:00
parent bee3231ed4
commit 11eb88a2ea

View file

@ -12,6 +12,7 @@ import (
) )
type lockContext struct { type lockContext struct {
lock *restic.Lock
cancel context.CancelFunc cancel context.CancelFunc
refreshWG sync.WaitGroup refreshWG sync.WaitGroup
} }
@ -104,6 +105,7 @@ retryLoop:
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
lockInfo := &lockContext{ lockInfo := &lockContext{
lock: lock,
cancel: cancel, cancel: cancel,
} }
lockInfo.refreshWG.Add(2) lockInfo.refreshWG.Add(2)
@ -112,8 +114,8 @@ retryLoop:
globalLocks.Lock() globalLocks.Lock()
globalLocks.locks[lock] = lockInfo globalLocks.locks[lock] = lockInfo
go refreshLocks(ctx, lock, lockInfo, refreshChan, forcedRefreshChan) go refreshLocks(ctx, lockInfo, refreshChan, forcedRefreshChan)
go monitorLockRefresh(ctx, lock, lockInfo, refreshChan, forcedRefreshChan) go monitorLockRefresh(ctx, lockInfo, refreshChan, forcedRefreshChan)
globalLocks.Unlock() globalLocks.Unlock()
return lock, ctx, err return lock, ctx, err
@ -125,8 +127,9 @@ var refreshInterval = 5 * time.Minute
// the difference allows to compensate for a small time drift between clients. // the difference allows to compensate for a small time drift between clients.
var refreshabilityTimeout = restic.StaleLockTimeout - refreshInterval*3/2 var refreshabilityTimeout = restic.StaleLockTimeout - refreshInterval*3/2
func refreshLocks(ctx context.Context, lock *restic.Lock, lockInfo *lockContext, refreshed chan<- struct{}, forcedRefresh <-chan struct{}) { func refreshLocks(ctx context.Context, lockInfo *lockContext, refreshed chan<- struct{}, forcedRefresh <-chan struct{}) {
debug.Log("start") debug.Log("start")
lock := lockInfo.lock
ticker := time.NewTicker(refreshInterval) ticker := time.NewTicker(refreshInterval)
lastRefresh := lock.Time lastRefresh := lock.Time
@ -177,7 +180,7 @@ func refreshLocks(ctx context.Context, lock *restic.Lock, lockInfo *lockContext,
} }
} }
func monitorLockRefresh(ctx context.Context, lock *restic.Lock, lockInfo *lockContext, refreshed <-chan struct{}, forcedRefresh chan<- struct{}) { func monitorLockRefresh(ctx context.Context, lockInfo *lockContext, refreshed <-chan struct{}, forcedRefresh chan<- struct{}) {
// time.Now() might use a monotonic timer which is paused during standby // time.Now() might use a monotonic timer which is paused during standby
// convert to unix time to ensure we compare real time values // convert to unix time to ensure we compare real time values
lastRefresh := time.Now().UnixNano() lastRefresh := time.Now().UnixNano()
@ -209,7 +212,7 @@ func monitorLockRefresh(ctx context.Context, lock *restic.Lock, lockInfo *lockCo
} }
// keep on going if our current lock still exists // keep on going if our current lock still exists
if tryRefreshStaleLock(ctx, lock) { if tryRefreshStaleLock(ctx, lockInfo.lock) {
lastRefresh = time.Now().UnixNano() lastRefresh = time.Now().UnixNano()
// inform refresh gorountine about forced refresh // inform refresh gorountine about forced refresh