Skip to content

Commit 0a60c9c

Browse files
Make generic interface (#1)
* Make generic interface * add grayburd redis client example * update examples and docs * update docs
1 parent 7b3f80d commit 0a60c9c

File tree

16 files changed

+762
-173
lines changed

16 files changed

+762
-173
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
examples/garyburd/vendor
2+
examples/goredis/vendor

README.md

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,30 @@
22

33
[![Build Status](https://travis-ci.com/dineshgowda24/redislock.svg)](https://travis-ci.com/dineshgowda24/redislock)
44
[![GoDoc](https://godoc.org/github.com/dineshgowda24/redislock?status.png)](http://godoc.org/github.com/bsm/redislock)
5-
[![Go Report Card](https://goreportcard.com/badge/github.com/bsm/redislock)](https://goreportcard.com/report/github.com/bsm/redislock)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/dineshgowda24/redislock)](https://goreportcard.com/report/github.com/bsm/redislock)
66
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
77

88
Simplified distributed locking implementation using [Redis](http://redis.io/topics/distlock).
99
For more information, please see examples.
1010

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
3512

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`.
4314

44-
// Don't forget to defer Release.
45-
defer lock.Release()
46-
fmt.Println("I have a lock!")
15+
## Features
4716

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.
5520

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
7322

74-
}
75-
```
23+
Check out examples in for [`garyburd`](./examples/garyburd) and [`go-redis`](./examples/goredis) clients.
7624

7725
## Documentation
7826

7927
Full documentation is available on [GoDoc](http://godoc.org/github.com/dineshgowda24/redislock)
28+
29+
## Contribution
30+
31+
Feel free to send a PR.

README.md.tpl

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/garyburd/Gopkg.lock

Lines changed: 182 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/garyburd/Gopkg.toml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
branch = "interface"
30+
name = "github.com/dineshgowda24/redislock"
31+
32+
[[constraint]]
33+
name = "github.com/garyburd/redigo"
34+
version = "1.6.0"
35+
36+
[[constraint]]
37+
name = "github.com/onsi/ginkgo"
38+
version = "1.14.0"
39+
40+
[[constraint]]
41+
name = "github.com/onsi/gomega"
42+
version = "1.10.1"
43+
44+
[prune]
45+
go-tests = true
46+
unused-packages = true

examples/garyburd/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Garyburd redigo
2+
3+
```
4+
type RedisLockClient struct {
5+
pool *redis.Pool
6+
luaRefresh *redis.Script
7+
luaPttl *redis.Script
8+
luaRelease *redis.Script
9+
}
10+
```
11+
12+
`RedisLockClient` implements `RedisClient` interface from `redislock.go`
13+
14+
## Installing the dependencies
15+
16+
```
17+
dep ensure -v
18+
```
19+
20+
## Running the application
21+
22+
```
23+
go run main.go
24+
```

0 commit comments

Comments
 (0)