Skip to content

Commit d4cdbe1

Browse files
jtackaberryucirello
authored andcommitted
dynamolock: improve cancelability of acquireLock
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
1 parent 829fa9a commit d4cdbe1

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

CONTRIBUTORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ Pawan Bishnoi [email protected]
55
Alexandre Picard-Lemieux [email protected]
66
Charlie Vieth ([email protected])
77
Daniel Gil ([email protected])
8-
David Becher ([email protected])
8+
David Becher ([email protected])
9+
Jason Tackaberry ([email protected]))

client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,18 @@ func (c *Client) acquireLock(ctx context.Context, opt *acquireLockOptions) (*Loc
317317
}
318318

319319
for {
320-
if err := ctx.Err(); err != nil {
321-
return nil, err
322-
}
323320
l, err := c.storeLock(ctx, &getLockOptions)
324321
if err != nil {
325322
return nil, err
326323
} else if l != nil {
327324
return l, nil
328325
}
329326
c.logger.Println("Sleeping for a refresh period of ", getLockOptions.refreshPeriodDuration)
330-
time.Sleep(getLockOptions.refreshPeriodDuration)
327+
select {
328+
case <-ctx.Done():
329+
return nil, ctx.Err()
330+
case <-time.After(getLockOptions.refreshPeriodDuration):
331+
}
331332
}
332333
}
333334

0 commit comments

Comments
 (0)