Skip to content

Commit

Permalink
refactor into minDuration func
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Feb 19, 2023
1 parent 22df7a3 commit 5dfa011
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions cmd/restic/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ var (
retrySleepMax = 60 * time.Second
)

func minDuration(a, b time.Duration) time.Duration {
if a <= b {
return a
}
return b
}

// lockRepository wraps the ctx such that it is cancelled when the repository is unlocked
// cancelling the original context also stops the lock refresh
func lockRepository(ctx context.Context, repo restic.Repository, exclusive bool, retryLock time.Duration, json bool) (*restic.Lock, context.Context, error) {
Expand All @@ -51,10 +58,7 @@ func lockRepository(ctx context.Context, repo restic.Repository, exclusive bool,
var lock *restic.Lock
var err error

retrySleep := retrySleepStart
if retrySleep > retryLock {
retrySleep = retryLock
}
retrySleep := minDuration(retrySleepStart, retryLock)
retryMessagePrinted := false
retryTimeout := time.After(retryLock)

Expand Down Expand Up @@ -82,12 +86,7 @@ retryLoop:
lock, err = lockFn(ctx, repo)
break retryLoop
case <-retrySleepCh:
retrySleepNext := retrySleep * 2
if retrySleepNext > retrySleepMax {
retrySleep = retrySleepMax
} else {
retrySleep = retrySleepNext
}
retrySleep = minDuration(retrySleep*2, retrySleepMax)
}
} else {
// anything else, either a successful lock or another error
Expand Down

0 comments on commit 5dfa011

Please sign in to comment.