give LockWithContext a little backoff

Backoff the Lock calls exponentially, to a reasonable limit.
pull/13262/head
James Bardin 9 years ago
parent 5eca913b14
commit 93b1dd6323

@ -75,8 +75,10 @@ type Locker interface {
}
// Lock the state, using the provided context for timeout and cancellation.
// TODO: this should probably backoff somewhat.
// This backs off slightly to an upper limit.
func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, error) {
delay := time.Second
maxDelay := 16 * time.Second
for {
id, err := s.Lock(info)
if err == nil {
@ -99,7 +101,10 @@ func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, erro
case <-ctx.Done():
// return the last lock error with the info
return "", err
case <-time.After(time.Second):
case <-time.After(delay):
if delay < maxDelay {
delay *= 2
}
}
}
}

Loading…
Cancel
Save