Skip to content

Commit

Permalink
dynamolock: improve cancelability of acquireLock
Browse files Browse the repository at this point in the history
When acquireLock() begins sleeping before a retry, it previously would not
return until after the full sleep interval had elapsed, which would cause the
caller to block for up to the configurable refresh period even after
cancellation.  This change allows immediate return should the context be
cancelled while sleeping.

Addresses #106
  • Loading branch information
jtackaberry authored and ucirello committed Sep 1, 2020
1 parent 829fa9a commit d4cdbe1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Pawan Bishnoi [email protected]
Alexandre Picard-Lemieux [email protected]
Charlie Vieth ([email protected])
Daniel Gil ([email protected])
David Becher ([email protected])
David Becher ([email protected])
Jason Tackaberry ([email protected]))
9 changes: 5 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,18 @@ func (c *Client) acquireLock(ctx context.Context, opt *acquireLockOptions) (*Loc
}

for {
if err := ctx.Err(); err != nil {
return nil, err
}
l, err := c.storeLock(ctx, &getLockOptions)
if err != nil {
return nil, err
} else if l != nil {
return l, nil
}
c.logger.Println("Sleeping for a refresh period of ", getLockOptions.refreshPeriodDuration)
time.Sleep(getLockOptions.refreshPeriodDuration)
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(getLockOptions.refreshPeriodDuration):
}
}
}

Expand Down

0 comments on commit d4cdbe1

Please sign in to comment.