@@ -35,12 +35,7 @@ type CancelFunc func()
35
35
// 该方法会立即返回锁定成功与否的结果
36
36
func RedisTryLock (key string ) bool {
37
37
ctx , cancel := context .WithTimeout (context .Background (), redisExecuteTimeout )
38
- go func () {
39
- for range time .After (redisExecuteTimeout ) {
40
- cancel ()
41
- break
42
- }
43
- }()
38
+ defer cancel ()
44
39
45
40
return RedisTryLockWithContext (ctx , key )
46
41
}
@@ -143,12 +138,7 @@ func RedisUnlockWithContext(ctx context.Context, key string) {
143
138
// using SetNX
144
139
func RedisStandaloneLock (key string ) bool {
145
140
ctx , cancel := context .WithTimeout (context .Background (), redisExecuteTimeout )
146
- go func () {
147
- for range time .After (redisExecuteTimeout ) {
148
- cancel ()
149
- break
150
- }
151
- }()
141
+ defer cancel ()
152
142
153
143
return RedisStandaloneLockWithContext (ctx , key )
154
144
}
@@ -197,12 +187,7 @@ func RedisStandaloneLockWithContext(ctx context.Context, key string) bool {
197
187
// using SetNX
198
188
func RedisStandaloneUnlock (key string ) {
199
189
ctx , cancel := context .WithTimeout (context .Background (), redisExecuteTimeout )
200
- go func () {
201
- for range time .After (redisExecuteTimeout ) {
202
- cancel ()
203
- break
204
- }
205
- }()
190
+ defer cancel ()
206
191
207
192
RedisStandaloneUnlockWithContext (ctx , key )
208
193
}
@@ -232,12 +217,7 @@ func RedisStandaloneUnlockWithContext(ctx context.Context, key string) {
232
217
// using SetNX
233
218
func RedisClusterLock (key string ) bool {
234
219
ctx , cancel := context .WithTimeout (context .Background (), redisExecuteTimeout )
235
- go func () {
236
- for range time .After (redisExecuteTimeout ) {
237
- cancel ()
238
- break
239
- }
240
- }()
220
+ defer cancel ()
241
221
242
222
return RedisClusterLockWithContext (ctx , key )
243
223
}
@@ -286,12 +266,7 @@ func RedisClusterLockWithContext(ctx context.Context, key string) bool {
286
266
// using SetNX
287
267
func RedisClusterUnlock (key string ) {
288
268
ctx , cancel := context .WithTimeout (context .Background (), redisExecuteTimeout )
289
- go func () {
290
- for range time .After (redisExecuteTimeout ) {
291
- cancel ()
292
- break
293
- }
294
- }()
269
+ defer cancel ()
295
270
296
271
RedisClusterUnlockWithContext (ctx , key )
297
272
}
0 commit comments