|
2 | 2 |
|
3 | 3 | [](https://travis-ci.com/dineshgowda24/redislock)
|
4 | 4 | [](http://godoc.org/github.com/bsm/redislock)
|
5 |
| -[](https://goreportcard.com/report/github.com/bsm/redislock) |
| 5 | +[](https://goreportcard.com/report/github.com/bsm/redislock) |
6 | 6 | [](https://opensource.org/licenses/Apache-2.0)
|
7 | 7 |
|
8 | 8 | Simplified distributed locking implementation using [Redis](http://redis.io/topics/distlock).
|
9 | 9 | For more information, please see examples.
|
10 | 10 |
|
11 |
| -## Examples |
12 |
| - |
13 |
| -```go |
14 |
| -import ( |
15 |
| - "fmt" |
16 |
| - "time" |
17 |
| - |
18 |
| - "github.com/bsm/redislock" |
19 |
| - "github.com/go-redis/redis/v7" |
20 |
| -) |
21 |
| - |
22 |
| -func main() { |
23 |
| - // Connect to redis. |
24 |
| - client := &redis.Pool{ |
25 |
| - MaxIdle: 3, |
26 |
| - IdleTimeout: 240 * time.Second, |
27 |
| - Dial: func() (redis.Conn, error) { |
28 |
| - return redis.Dial("tcp", ":6379", |
29 |
| - redis.DialDatabase(1)) |
30 |
| - }, |
31 |
| - } |
32 |
| - |
33 |
| - // Create a new lock client. |
34 |
| - locker := redislock.New(client) |
| 11 | +## Motivation |
35 | 12 |
|
36 |
| - // Try to obtain lock. |
37 |
| - lock, err := locker.Obtain("my-key", 100*time.Millisecond, nil) |
38 |
| - if err == redislock.ErrNotObtained { |
39 |
| - fmt.Println("Could not obtain lock!") |
40 |
| - } else if err != nil { |
41 |
| - log.Fatalln(err) |
42 |
| - } |
| 13 | +I came across a concurrency issue when multiple clients were accessing single redis instance. So I wanted a primitive locking solution, but redis did not have one implemented. So started looking for open source libraries and found [redislock](https://github.com/bsm/redislock) very well written and effective library. But it still did not solve my problem as I was using [redigo](https://github.com/garyburd/redigo) client but the package used [go-redis](https://github.com/go-redis/redis). Although `redigo` had [`redsync`](https://github.com/go-redsync/redsync), I wanted a much more simpler one and so with `redislock`. |
43 | 14 |
|
44 |
| - // Don't forget to defer Release. |
45 |
| - defer lock.Release() |
46 |
| - fmt.Println("I have a lock!") |
| 15 | +## Features |
47 | 16 |
|
48 |
| - // Sleep and check the remaining TTL. |
49 |
| - time.Sleep(50 * time.Millisecond) |
50 |
| - if ttl, err := lock.TTL(); err != nil { |
51 |
| - log.Fatalln(err) |
52 |
| - } else if ttl > 0 { |
53 |
| - fmt.Println("Yay, I still have my lock!") |
54 |
| - } |
| 17 | + - Simple and easy to use interface. |
| 18 | + - Plug in any redis client of your choice by implementing the `RedisClient` interface. |
| 19 | + - Simple but effective locking for single redis instance. |
55 | 20 |
|
56 |
| - // Extend my lock. |
57 |
| - if err := lock.Refresh(100*time.Millisecond, nil); err != nil { |
58 |
| - log.Fatalln(err) |
59 |
| - } |
60 |
| - |
61 |
| - // Sleep a little longer, then check. |
62 |
| - time.Sleep(100 * time.Millisecond) |
63 |
| - if ttl, err := lock.TTL(); err != nil { |
64 |
| - log.Fatalln(err) |
65 |
| - } else if ttl == 0 { |
66 |
| - fmt.Println("Now, my lock has expired!") |
67 |
| - } |
68 |
| - |
69 |
| - // Output: |
70 |
| - // I have a lock! |
71 |
| - // Yay, I still have my lock! |
72 |
| - // Now, my lock has expired! |
| 21 | +## Examples |
73 | 22 |
|
74 |
| -} |
75 |
| -``` |
| 23 | +Check out examples in for [`garyburd`](./examples/garyburd) and [`go-redis`](./examples/goredis) clients. |
76 | 24 |
|
77 | 25 | ## Documentation
|
78 | 26 |
|
79 | 27 | Full documentation is available on [GoDoc](http://godoc.org/github.com/dineshgowda24/redislock)
|
| 28 | + |
| 29 | +## Contribution |
| 30 | + |
| 31 | +Feel free to send a PR. |
0 commit comments