-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_lock.go
172 lines (154 loc) · 4.45 KB
/
count_lock.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package glock
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/go-redis/redis/v8"
)
type ICountLock interface {
// Start is config start
Start(key interface{}, startWith int, expired time.Duration) error
// DecrBy decrease count
DecrBy(key interface{}, count int64) (int, error)
// Current get current count
Current(key interface{}) (int, error)
// Existed check key existed
Existed(key interface{}) (bool, error)
// IncrBy increase count
IncrBy(key interface{}, count int64) (int, error)
// IncrBy stop counter
StopCounter(key interface{}) error
}
type CountLock struct {
client *redis.Client
timelock time.Duration
prefix string
}
// StartCountLock
func StartCountLock(cf *ConnectConfig) (*CountLock, error) {
client := redis.NewClient(&redis.Options{
Addr: cf.RedisAddr,
Password: cf.RedisPw,
MaxRetries: 10,
MinRetryBackoff: 15 * time.Millisecond,
MaxRetryBackoff: 1000 * time.Millisecond,
DialTimeout: 10 * time.Second,
DB: cf.RedisDb, // use default DB
})
if cf.Timelock < 0 {
return nil, errors.New("timelock is required")
}
ctx, cancel := context.WithTimeout(context.Background(), cf.Timelock)
defer cancel()
if err := client.Ping(ctx).Err(); err != nil {
return nil, err
}
return &CountLock{
client: client,
prefix: cf.Prefix,
timelock: cf.Timelock,
}, nil
}
// CreateCountLock deprecated
func CreateCountLock(redisAddr, redisPw, prefix string, timelock time.Duration) (*CountLock, error) {
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
Password: redisPw,
MaxRetries: 10,
MinRetryBackoff: 15 * time.Millisecond,
MaxRetryBackoff: 1000 * time.Millisecond,
DialTimeout: 10 * time.Second,
DB: 1, // use default DB
})
if timelock < 0 {
return nil, errors.New("timelock is required")
}
ctx, cancel := context.WithTimeout(context.Background(), timelock)
defer cancel()
if err := client.Ping(ctx).Err(); err != nil {
return nil, err
}
return &CountLock{
client: client,
prefix: prefix,
timelock: timelock,
}, nil
}
// Start: khởi động 1 tiến trình bộ đếm. bắt đầu bằng startWith và thời hạn hiệu lực của bộ đếm là expired
// Start có thể dùng để reset counter về 1 giá trị nào đó.
func (cl *CountLock) Start(key interface{}, startWith int, expired time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), cl.timelock)
defer cancel()
rdKey := fmt.Sprintf("%v_%v", cl.prefix, key)
if err := cl.client.Set(ctx, rdKey, startWith, expired).Err(); err != nil {
return err
}
if expired == -1 {
if err := cl.client.Persist(ctx, rdKey).Err(); err != nil {
log.Print(err)
return errors.New(StatusNotPersist)
}
}
return nil
}
func (cl *CountLock) DecrBy(key interface{}, count int64) (int, error) {
if count <= 0 {
return 0, errors.New(StatusInvalidCounter)
}
ctx, cancel := context.WithTimeout(context.Background(), cl.timelock)
defer cancel()
rdKey := fmt.Sprintf("%s_%v", cl.prefix, key)
curVal, err := cl.client.DecrBy(ctx, rdKey, count).Result()
if err != nil {
return 0, err
}
return int(curVal), nil
}
func (cl *CountLock) Current(key interface{}) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), cl.timelock)
defer cancel()
rdKey := fmt.Sprintf("%s_%v", cl.prefix, key)
current, err := cl.client.Get(ctx, rdKey).Int()
if err != nil {
return 0, err
}
return current, nil
}
func (cl *CountLock) IncrBy(key interface{}, count int64) (int, error) {
if count <= 0 {
return 0, errors.New(StatusInvalidCounter)
}
ctx, cancel := context.WithTimeout(context.Background(), cl.timelock)
defer cancel()
rdKey := fmt.Sprintf("%s_%v", cl.prefix, key)
curVal, err := cl.client.IncrBy(ctx, rdKey, count).Result()
if err != nil {
return 0, err
}
return int(curVal), nil
}
func (cl *CountLock) StopCounter(key interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), cl.timelock)
defer cancel()
rdKey := fmt.Sprintf("%s_%v", cl.prefix, key)
err := cl.client.Del(ctx, rdKey).Err()
if err != nil {
return err
}
return err
}
func (cl *CountLock) Existed(key interface{}) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), cl.timelock)
defer cancel()
rdKey := fmt.Sprintf("%s_%v", cl.prefix, key)
val, err := cl.client.Exists(ctx, rdKey).Result()
if err != nil {
return false, err
}
if val == 0 {
return false, nil
}
return true, nil
}