Skip to content

Commit a51ac37

Browse files
committed
context cancel optimize
1 parent be6de83 commit a51ac37

File tree

1 file changed

+2
-13
lines changed

1 file changed

+2
-13
lines changed

redislock.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,7 @@ func (rd *redisDriver) tryLock(key string) bool {
109109
}
110110

111111
ctx, cancel := context.WithTimeout(context.Background(), redisExecuteTimeout)
112-
go func() {
113-
for range time.After(redisExecuteTimeout) {
114-
cancel()
115-
break
116-
}
117-
}()
112+
defer cancel()
118113

119114
var (
120115
ok bool
@@ -176,13 +171,7 @@ func (rd *redisDriver) unlock(key string) {
176171
}
177172

178173
ctx, cancel := context.WithTimeout(context.Background(), redisExecuteTimeout)
179-
180-
go func() {
181-
for range time.After(redisExecuteTimeout) {
182-
cancel()
183-
break
184-
}
185-
}()
174+
defer cancel()
186175

187176
if rd.client != nil {
188177
_, _ = rd.client.Del(ctx, key).Result()

0 commit comments

Comments
 (0)