-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdlock.go
81 lines (65 loc) · 2.2 KB
/
cmdlock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package redmos
import (
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
const lockFormat = "lock:%v" // 鎖定/解鎖索引格式
// Lock 鎖定行為, 以索引字串到主要資料庫中執行分布式鎖定, 避免同時執行客戶端動作, 使用上有以下幾點須注意
// - 執行前設定好 Key 並且不能為空字串
// - 鎖定完成後, 需要執行 Unlock 行為來解除鎖定
// - 鎖定後會在 Timeout 之後自動解鎖, 避免死鎖
type Lock struct {
Behave // 行為物件
Key string // 索引值
time time.Duration // 超時時間, redis的超時時間不能低於1秒; 這個欄位是為了讓單元測試可以設定較短的超時時間
incr *redis.IntCmd // 遞增命令結果
expire *redis.BoolCmd // 超時命令結果
}
// Prepare 前置處理
func (this *Lock) Prepare() error {
if this.Key == "" {
return fmt.Errorf("lock prepare: key empty")
} // if
key := fmt.Sprintf(lockFormat, this.Key)
this.incr = this.Major().Incr(this.Ctx(), key)
this.expire = this.Major().Expire(this.Ctx(), key, this.time)
return nil
}
// Complete 完成處理
func (this *Lock) Complete() error {
data, err := this.incr.Result()
if err != nil {
return fmt.Errorf("lock complete: %w: %v", err, this.Key)
} // if
if data != 1 {
return fmt.Errorf("lock complete: already lock: %v", this.Key)
} // if
if _, err = this.expire.Result(); err != nil {
return fmt.Errorf("lock complete: %w: %v", err, this.Key)
} // if
return nil
}
// Unlock 解鎖行為, 解除被 Lock 行為鎖定的索引, 使用上有以下幾點須注意
// - 執行前設定好 Key 並且不能為空字串
type Unlock struct {
Behave // 行為物件
Key string // 索引值
del *redis.IntCmd // 刪除命令結果
}
// Prepare 前置處理
func (this *Unlock) Prepare() error {
if this.Key == "" {
return fmt.Errorf("unlock prepare: key empty")
} // if
key := fmt.Sprintf(lockFormat, this.Key)
this.del = this.Major().Del(this.Ctx(), key)
return nil
}
// Complete 完成處理
func (this *Unlock) Complete() error {
if _, err := this.del.Result(); err != nil {
return fmt.Errorf("unlock complete: %w: %v", err, this.Key)
} // if
return nil
}