feat(redislock): support set context (#2208)

* feat(redislock): support set context

* chore: fix test
This commit is contained in:
cong
2022-07-30 18:38:36 +08:00
committed by GitHub
parent b18479dd43
commit 4879d4dfcd
2 changed files with 54 additions and 20 deletions

View File

@@ -1,12 +1,14 @@
package redis
import (
"context"
"math/rand"
"strconv"
"sync/atomic"
"time"
red "github.com/go-redis/redis/v8"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stringx"
)
@@ -34,6 +36,7 @@ type RedisLock struct {
seconds uint32
key string
id string
ctx context.Context
}
func init() {
@@ -51,8 +54,9 @@ func NewRedisLock(store *Redis, key string) *RedisLock {
// Acquire acquires the lock.
func (rl *RedisLock) Acquire() (bool, error) {
rl.fillCtx()
seconds := atomic.LoadUint32(&rl.seconds)
resp, err := rl.store.Eval(lockCommand, []string{rl.key}, []string{
resp, err := rl.store.EvalCtx(rl.ctx, lockCommand, []string{rl.key}, []string{
rl.id, strconv.Itoa(int(seconds)*millisPerSecond + tolerance),
})
if err == red.Nil {
@@ -75,7 +79,8 @@ func (rl *RedisLock) Acquire() (bool, error) {
// Release releases the lock.
func (rl *RedisLock) Release() (bool, error) {
resp, err := rl.store.Eval(delCommand, []string{rl.key}, []string{rl.id})
rl.fillCtx()
resp, err := rl.store.EvalCtx(rl.ctx, delCommand, []string{rl.key}, []string{rl.id})
if err != nil {
return false, err
}
@@ -92,3 +97,14 @@ func (rl *RedisLock) Release() (bool, error) {
func (rl *RedisLock) SetExpire(seconds int) {
atomic.StoreUint32(&rl.seconds, uint32(seconds))
}
// WithContext set context.
func (rl *RedisLock) WithContext(ctx context.Context) {
rl.ctx = ctx
}
func (rl *RedisLock) fillCtx() {
if rl.ctx == nil {
rl.ctx = context.Background()
}
}