https://github.com/adrian-lin-1-0-0/redlock
redlock 讀書筆記與demo
https://github.com/adrian-lin-1-0-0/redlock
Last synced: 3 months ago
JSON representation
redlock 讀書筆記與demo
- Host: GitHub
- URL: https://github.com/adrian-lin-1-0-0/redlock
- Owner: adrian-lin-1-0-0
- Created: 2023-12-10T16:45:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-10T16:59:15.000Z (over 1 year ago)
- Last Synced: 2025-01-25T18:43:41.417Z (5 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RedLock
> [distributed-locks](https://redis.io/docs/manual/patterns/distributed-locks/)透過 github.com/redis/go-redis/v9 簡單實作 RedLock
如果你需要使用到`RedLock`,請使用 [redsync](https://github.com/go-redsync/redsync)
因為更新`Database`的資料前,可能遇到`stop the world gc`,所以還需要搭配
樂觀鎖或其他方式來避免`stop the world gc`之後鎖過期被取走的問題:```go
rl := redlock.New(
&redlock.Options{
Addr: "localhost:6379",
},
&redlock.Options{
Addr: "localhost:6380",
},
)m := rl.NewMutex("test1", 10*time.Second)
err := m.Lock()
if err != nil {
log.Default().Println(err)
m.Unlock()
return
}mysql := NewMysql()
data := mysql.GetTestById(1)
version := data.Version
newVersion := version + 1
mysql.UpdateTest.
Where("id", 1).
Where("version", version).
Set("version", newVersion).
Set("value", "new value").
Exec()m.Unlock()
```## Redlock分析
Martin Kleppmann的[分析](https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html)
[與此分析相反的觀點](http://antirez.com/news/101)