Skip to content

Commit

Permalink
Allow custom deadlines, re-implement tests using stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
dim committed Dec 29, 2021
1 parent 422cf2a commit a2b6249
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 189 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![Test](https://github.com/bsm/redislock/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/redislock/actions/workflows/test.yml)
[![GoDoc](https://godoc.org/github.com/bsm/redislock?status.png)](http://godoc.org/github.com/bsm/redislock)
[![Go Report Card](https://goreportcard.com/badge/github.com/bsm/redislock)](https://goreportcard.com/report/github.com/bsm/redislock)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

Simplified distributed locking implementation using [Redis](http://redis.io/topics/distlock).
Expand Down
1 change: 0 additions & 1 deletion README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![Test](https://github.com/bsm/redislock/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/redislock/actions/workflows/test.yml)
[![GoDoc](https://godoc.org/github.com/bsm/redislock?status.png)](http://godoc.org/github.com/bsm/redislock)
[![Go Report Card](https://goreportcard.com/badge/github.com/bsm/redislock)](https://goreportcard.com/report/github.com/bsm/redislock)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

Simplified distributed locking implementation using [Redis](http://redis.io/topics/distlock).
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ module github.com/bsm/redislock

go 1.13

require (
github.com/bsm/ginkgo v1.16.4
github.com/bsm/gomega v1.16.0
github.com/go-redis/redis/v8 v8.11.4
)
require github.com/go-redis/redis/v8 v8.11.4
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
github.com/bsm/ginkgo v1.16.4 h1:pkHpo2VJRvI0NGlxCYi8qovww76L7+g82MgM+UBvH4A=
github.com/bsm/ginkgo v1.16.4/go.mod h1:RabIZLzOCPghgHJKUqHZpqrQETA5AnF4aCSIYy5C1bk=
github.com/bsm/gomega v1.16.0 h1:LEoRGHyYl3MqAcXgczKX/C3bxlxjl3gjP37PGvPNplw=
github.com/bsm/gomega v1.16.0/go.mod h1:JifAceMQ4crZIWYUKrlGcmbN3bqHogVTADMD2ATsbwk=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
12 changes: 8 additions & 4 deletions redislock.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ func (c *Client) Obtain(ctx context.Context, key string, ttl time.Duration, opt
value := token + opt.getMetadata()
retry := opt.getRetryStrategy()

deadlinectx, cancel := context.WithDeadline(ctx, time.Now().Add(ttl))
defer cancel()
// make sure we don't retry forever
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(ttl))
defer cancel()
}

var timer *time.Timer
for {
ok, err := c.obtain(deadlinectx, key, value, ttl)
ok, err := c.obtain(ctx, key, value, ttl)
if err != nil {
return nil, err
} else if ok {
Expand All @@ -86,7 +90,7 @@ func (c *Client) Obtain(ctx context.Context, key string, ttl time.Duration, opt
}

select {
case <-deadlinectx.Done():
case <-ctx.Done():
return nil, ErrNotObtained
case <-timer.C:
}
Expand Down
Loading

0 comments on commit a2b6249

Please sign in to comment.