Skip to content

Commit e7980fa

Browse files
authored
Merge pull request #52 from troyhantech/develop
Develop
2 parents 9e7a31f + 5becd02 commit e7980fa

13 files changed

Lines changed: 372 additions & 135 deletions

CHANGELOG.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ChangeLog
2+
23
## [Unreleased]
4+
35
基于 redis 实现:分布式“互斥锁”和“读写锁”。
46

57
目前不支持可重入锁、红锁。
@@ -8,11 +10,11 @@
810

911
## 互斥锁
1012

11-
* 任一时刻,对于一把锁,只可以有一个持有者。
13+
- 任一时刻,对于一把锁,只可以有一个持有者。
1214

1315
## 读写锁
1416

15-
* 读锁与读锁可以共存,写锁与读锁/写锁不可以共存。
17+
- 读锁与读锁可以共存,写锁与读锁/写锁不可以共存。
1618

1719
> 无论是互斥锁还是读写锁,都只可以由加锁的协程解锁,其他协程无法解锁。
1820
> 加锁成功以后会开启一个协程定时续锁,直到客户端解锁。
@@ -22,7 +24,7 @@
2224
## 获取依赖
2325

2426
```shell
25-
go get github.com/MaricoHan/redisson
27+
go get github.com/troyhantech/redisson
2628
```
2729

2830
## 使用互斥锁
@@ -34,7 +36,7 @@ import (
3436
"context"
3537
"log"
3638

37-
"github.com/MaricoHan/redisson"
39+
"github.com/troyhantech/redisson"
3840
"github.com/go-redis/redis/v8"
3941
)
4042

@@ -80,7 +82,7 @@ import (
8082
"context"
8183
"log"
8284

83-
"github.com/MaricoHan/redisson"
85+
"github.com/troyhantech/redisson"
8486
"github.com/go-redis/redis/v8"
8587
)
8688

@@ -124,7 +126,7 @@ import (
124126
"context"
125127
"log"
126128

127-
"github.com/MaricoHan/redisson"
129+
"github.com/troyhantech/redisson"
128130
"github.com/go-redis/redis/v8"
129131
)
130132

@@ -171,7 +173,6 @@ options := []mutex.Option{
171173
rwMutex := r.NewRWMutex("rwMutexKey", options...) // 互斥锁同理
172174
```
173175

174-
175176
<!-- release links -->
176177

177-
[Unreleased]: https://github.com/MaricoHan/redisson/tree/master
178+
[Unreleased]: https://github.com/troyhantech/redisson/tree/master

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 MaricoHan
3+
Copyright (c) 2022 troyhantech
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.en.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# redisson
2+
3+
<div align="center">
4+
[![Go](https://github.com/troyhantech/redisson/actions/workflows/go.yml/badge.svg)](https://github.com/troyhantech/redisson/actions/workflows/go.yml)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/troyhantech/redisson)](https://goreportcard.com/report/github.com/troyhantech/redisson)
6+
[![License: MIT](https://img.shields.io/github/license/troyhantech/redisson)](https://github.com/troyhantech/redisson/blob/master/LICENSE)
7+
English | [中文](README.md)
8+
9+
</div>
10+
11+
Distributed mutex and read-write locks implemented on top of Redis (Go).
12+
13+
Currently does not support re-entrant locks or Redlock.
14+
15+
## Features
16+
17+
### Mutex
18+
19+
- At any moment, only one holder can own a given lock.
20+
21+
### Read-Write Lock
22+
23+
- Multiple read locks can coexist; write lock is exclusive and cannot coexist with read or write locks.
24+
25+
> Only the goroutine that acquired the lock can unlock it. After a lock is acquired, a background goroutine will periodically extend the lock (renewal) until the client unlocks it.
26+
27+
## Usage
28+
29+
### Install
30+
31+
```shell
32+
go get github.com/troyhantech/redisson
33+
```
34+
35+
### Using Mutex
36+
37+
```go
38+
package main
39+
40+
import (
41+
"context"
42+
"log"
43+
44+
"github.com/troyhantech/redisson"
45+
"github.com/go-redis/redis/v8"
46+
)
47+
48+
func main() {
49+
// 1. Initialize Redis client
50+
client := redis.NewClient(&redis.Options{Addr: ":6379"})
51+
52+
// 2. Create a redisson instance with the client
53+
r := redisson.New(context.Background(), client)
54+
55+
// 3. Create a mutex
56+
mutex := r.NewMutex("mutexKey")
57+
58+
// 4. Acquire the lock
59+
err := mutex.Lock(context.Background())
60+
if err != nil {
61+
log.Panic(err)
62+
return
63+
}
64+
log.Println("lock successfully!")
65+
66+
// 5. Do your work here
67+
// ...
68+
69+
// 6. Unlock
70+
err = mutex.Unlock(context.Background())
71+
if err != nil {
72+
log.Panic(err)
73+
return
74+
}
75+
log.Println("unlock successfully!")
76+
}
77+
```
78+
79+
## Using Read-Write Lock
80+
81+
### Write Lock
82+
83+
```go
84+
package main
85+
86+
import (
87+
"context"
88+
"log"
89+
90+
"github.com/troyhantech/redisson"
91+
"github.com/go-redis/redis/v8"
92+
)
93+
94+
func main() {
95+
// 1. Initialize Redis client
96+
client := redis.NewClient(&redis.Options{Addr: ":6379"})
97+
98+
// 2. Create a redisson instance with the client
99+
r := redisson.New(context.Background(), client)
100+
101+
// 3. Create a RWMutex
102+
mutex := r.NewRWMutex("rwMutexKey")
103+
104+
// 4. Acquire write lock
105+
err := mutex.Lock(context.Background())
106+
if err != nil {
107+
log.Panic(err)
108+
return
109+
}
110+
log.Println("lock successfully!")
111+
112+
// 5. Do your work here
113+
// ...
114+
115+
// 6. Unlock
116+
err = mutex.Unlock(context.Background())
117+
if err != nil {
118+
log.Panic(err)
119+
return
120+
}
121+
log.Println("unlock successfully!")
122+
}
123+
```
124+
125+
### Read Lock
126+
127+
```go
128+
package main
129+
130+
import (
131+
"context"
132+
"log"
133+
134+
"github.com/troyhantech/redisson"
135+
"github.com/go-redis/redis/v8"
136+
)
137+
138+
func main() {
139+
// 1. Initialize Redis client
140+
client := redis.NewClient(&redis.Options{Addr: ":6379"})
141+
142+
// 2. Create a redisson instance with the client
143+
r := redisson.New(context.Background(), client)
144+
145+
// 3. Create a RWMutex
146+
rwMutex := r.NewRWMutex("rwMutexKey")
147+
148+
// 4. Acquire read lock
149+
err := rwMutex.RLock(context.Background())
150+
if err != nil {
151+
log.Panic(err)
152+
return
153+
}
154+
log.Println("rLock successfully!")
155+
156+
// 5. Do your work here
157+
// ...
158+
159+
// 6. Unlock
160+
err = rwMutex.Unlock(context.Background())
161+
if err != nil {
162+
log.Panic(err)
163+
return
164+
}
165+
log.Println("unlock successfully!")
166+
}
167+
```
168+
169+
### Options
170+
171+
You can configure optional settings. Defaults are `WaitTimeout = 30s` and `ExpireDuration = 10s`.
172+
173+
```go
174+
options := []mutex.Option{
175+
mutex.WithWaitTimeout(10 * time.Second), // Maximum wait time for acquiring the lock
176+
mutex.WithExpireDuration(20 * time.Second), // Lock expiration duration while held
177+
}
178+
rwMutex := r.NewRWMutex("rwMutexKey", options...) // Same for Mutex
179+
```
180+
181+
## License
182+
183+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
184+
185+
## 🤝 Contributing
186+
187+
We welcome all forms of contributions! Whether it's reporting bugs, suggesting new features, or submitting code improvements.
188+
189+
## 🐛 Issue Reporting
190+
191+
If you find bugs or have feature suggestions, please submit them on the [Issues](https://github.com/troyhantech/redisson/issues) page.
192+
193+
## ⭐ Star History
194+
195+
If this project helps you, please give us a ⭐!
196+
197+
[![Star History Chart](https://api.star-history.com/svg?repos=troyhantech/redisson&type=Date)](https://star-history.com/#troyhantech/redisson&Date)
198+
199+
---
200+
201+
<div align="center">
202+
<p>Made with ❤️ by the troyhantech</p>
203+
<p>If you like this project, please consider giving it a ⭐</p>
204+
</div>

0 commit comments

Comments
 (0)