|
| 1 | +## go-redisson |
| 2 | + |
| 3 | +一个借鉴 redisson 用 go 实现的 Redis 分布式锁的库。 |
| 4 | + |
| 5 | +## 安装 |
| 6 | + |
| 7 | +```shell |
| 8 | +go get github.com/cheerego/go-redisson |
| 9 | +``` |
| 10 | + |
| 11 | + |
| 12 | +## 支持锁类型 |
| 13 | + |
| 14 | +* Mutex [示例](#Mutex) 特性: |
| 15 | + * 互斥锁 (X Lock)。 |
| 16 | + * 和 go 标准库 sync.Mutex 用起来差不多。 |
| 17 | + * 不支持可重入。 |
| 18 | + * 支持 WatchDog。 |
| 19 | + |
| 20 | +* RLock[示例](#RLock) 特性: |
| 21 | + * 互斥可重入锁。 |
| 22 | + * 和 redisson 使用起来差不多。 |
| 23 | + * 支持同一个协程重复加锁。 |
| 24 | + * 支持 WatchDog。 |
| 25 | + |
| 26 | +## 特性 |
| 27 | + |
| 28 | +* tryLock,if waitTime > 0, wait `waitTime` milliseconds to try to obtain lock by while true and redis pub sub. |
| 29 | +* watchdog, if leaseTime = -1, start a time.Ticker(defaultWatchDogTime / 3) to renew lock expiration time. |
| 30 | + |
| 31 | +## 配置 |
| 32 | + |
| 33 | +### WatchDogTimeout |
| 34 | + |
| 35 | +```go |
| 36 | +g := godisson.NewGodisson(rdb, godisson.WithWatchDogTimeout(30*time.Second)) |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +## 示例 |
| 41 | + |
| 42 | +### Mutex |
| 43 | + |
| 44 | +```go |
| 45 | +package examples |
| 46 | + |
| 47 | +import ( |
| 48 | + "github.com/cheerego/godisson" |
| 49 | + "github.com/go-redis/redis/v8" |
| 50 | + "github.com/pkg/errors" |
| 51 | + "log" |
| 52 | + "time" |
| 53 | +) |
| 54 | + |
| 55 | +func main() { |
| 56 | + |
| 57 | + // create redis client |
| 58 | + rdb := redis.NewClient(&redis.Options{ |
| 59 | + Addr: "localhost:6379", |
| 60 | + Password: "", // no password set |
| 61 | + DB: 0, // use default DB |
| 62 | + }) |
| 63 | + defer rdb.Close() |
| 64 | + |
| 65 | + g := godisson.NewGodisson(rdb, godisson.WithWatchDogTimeout(30*time.Second)) |
| 66 | + |
| 67 | + test1(g) |
| 68 | + test2(g) |
| 69 | +} |
| 70 | + |
| 71 | +// can't obtain lock in a same goroutine |
| 72 | +func test1(g *godisson.Godisson) { |
| 73 | + m1 := g.NewMutex("godisson") |
| 74 | + m2 := g.NewMutex("godisson") |
| 75 | + |
| 76 | + err := m1.TryLock(-1, 20000) |
| 77 | + if errors.Is(err, godisson.ErrLockNotObtained) { |
| 78 | + log.Println("can't obtained lock") |
| 79 | + } else if err != nil { |
| 80 | + log.Fatalln(err) |
| 81 | + } |
| 82 | + defer m1.Unlock() |
| 83 | + |
| 84 | + // because waitTime = -1, waitTime < 0, try once, will return ErrLockNotObtained |
| 85 | + err = m2.TryLock(-1, 20000) |
| 86 | + if errors.Is(err, godisson.ErrLockNotObtained) { |
| 87 | + log.Println("m2 must not obtained lock") |
| 88 | + } else if err != nil { |
| 89 | + log.Fatalln(err) |
| 90 | + } |
| 91 | + time.Sleep(10 * time.Second) |
| 92 | +} |
| 93 | + |
| 94 | +func test2(g *godisson.Godisson) { |
| 95 | + m1 := g.NewMutex("godisson") |
| 96 | + m2 := g.NewMutex("godisson") |
| 97 | + |
| 98 | + go func() { |
| 99 | + err := m1.TryLock(-1, 20000) |
| 100 | + if errors.Is(err, godisson.ErrLockNotObtained) { |
| 101 | + log.Println("can't obtained lock") |
| 102 | + } else if err != nil { |
| 103 | + log.Fatalln(err) |
| 104 | + } |
| 105 | + time.Sleep(10 * time.Second) |
| 106 | + m1.Unlock() |
| 107 | + }() |
| 108 | + |
| 109 | + // waitTime > 0, after 10 milliseconds will obtain the lock |
| 110 | + go func() { |
| 111 | + time.Sleep(1 * time.Second) |
| 112 | + |
| 113 | + err := m2.TryLock(15000, 20000) |
| 114 | + if errors.Is(err, godisson.ErrLockNotObtained) { |
| 115 | + log.Println("m2 must not obtained lock") |
| 116 | + } else if err != nil { |
| 117 | + log.Fatalln(err) |
| 118 | + } |
| 119 | + time.Sleep(10 * time.Second) |
| 120 | + |
| 121 | + m2.Unlock() |
| 122 | + }() |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | +### RLock |
| 130 | +```go |
| 131 | +package examples |
| 132 | + |
| 133 | +import ( |
| 134 | + "github.com/cheerego/godisson" |
| 135 | + "github.com/go-redis/redis/v8" |
| 136 | + "log" |
| 137 | + "time" |
| 138 | +) |
| 139 | + |
| 140 | +func main() { |
| 141 | + |
| 142 | + // create redis client |
| 143 | + rdb := redis.NewClient(&redis.Options{ |
| 144 | + Addr: "localhost:6379", |
| 145 | + Password: "", // no password set |
| 146 | + DB: 0, // use default DB |
| 147 | + }) |
| 148 | + defer rdb.Close() |
| 149 | + |
| 150 | + g := godisson.NewGodisson(rdb, godisson.WithWatchDogTimeout(30*time.Second)) |
| 151 | + |
| 152 | + // lock with watchdog without retry |
| 153 | + lock := g.NewRLock("godisson") |
| 154 | + |
| 155 | + err := lock.Lock() |
| 156 | + if err == godisson.ErrLockNotObtained { |
| 157 | + log.Println("Could not obtain lock") |
| 158 | + } else if err != nil { |
| 159 | + log.Fatalln(err) |
| 160 | + } |
| 161 | + defer lock.Unlock() |
| 162 | + |
| 163 | + // lock with retry、watchdog |
| 164 | + // leaseTime value is -1, enable watchdog |
| 165 | + lock2 := g.NewRLock("godission-try-watchdog") |
| 166 | + |
| 167 | + err = lock2.TryLock(20000, -1) |
| 168 | + if err == godisson.ErrLockNotObtained { |
| 169 | + log.Println("Could not obtain lock") |
| 170 | + } else if err != nil { |
| 171 | + log.Fatalln(err) |
| 172 | + } |
| 173 | + time.Sleep(10 * time.Second) |
| 174 | + defer lock.Unlock() |
| 175 | +} |
| 176 | + |
| 177 | +``` |
0 commit comments