give LockWithContext a little backoff

Backoff the Lock calls exponentially, to a reasonable limit.
This commit is contained in:
James Bardin 2017-04-01 17:56:03 -04:00
parent 5eca913b14
commit 93b1dd6323

View File

@ -75,8 +75,10 @@ type Locker interface {
} }
// Lock the state, using the provided context for timeout and cancellation. // 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) { func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, error) {
delay := time.Second
maxDelay := 16 * time.Second
for { for {
id, err := s.Lock(info) id, err := s.Lock(info)
if err == nil { if err == nil {
@ -99,7 +101,10 @@ func LockWithContext(ctx context.Context, s State, info *LockInfo) (string, erro
case <-ctx.Done(): case <-ctx.Done():
// return the last lock error with the info // return the last lock error with the info
return "", err return "", err
case <-time.After(time.Second): case <-time.After(delay):
if delay < maxDelay {
delay *= 2
}
} }
} }
} }