Files
go-zero/core/stores/redis/redislock.go

107 lines
2.2 KiB
Go
Raw Permalink Normal View History

2020-07-26 17:09:05 +08:00
package redis
import (
"context"
_ "embed"
2024-03-08 22:35:17 +08:00
"errors"
2020-07-26 17:09:05 +08:00
"math/rand"
"strconv"
2020-07-26 17:09:05 +08:00
"sync/atomic"
"time"
red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stringx"
2020-07-26 17:09:05 +08:00
)
const (
randomLen = 16
tolerance = 500 // milliseconds
millisPerSecond = 1000
)
var (
//go:embed lockscript.lua
2024-04-17 23:37:35 +08:00
lockLuaScript string
lockScript = NewScript(lockLuaScript)
//go:embed delscript.lua
2024-04-17 23:37:35 +08:00
delLuaScript string
delScript = NewScript(delLuaScript)
2020-07-26 17:09:05 +08:00
)
// A RedisLock is a redis lock.
2020-07-26 17:09:05 +08:00
type RedisLock struct {
store *Redis
seconds uint32
key string
id string
}
func init() {
2024-03-02 22:45:24 +08:00
rand.NewSource(time.Now().UnixNano())
2020-07-26 17:09:05 +08:00
}
// NewRedisLock returns a RedisLock.
2020-07-26 17:09:05 +08:00
func NewRedisLock(store *Redis, key string) *RedisLock {
return &RedisLock{
store: store,
key: key,
id: stringx.Randn(randomLen),
2020-07-26 17:09:05 +08:00
}
}
// Acquire acquires the lock.
2020-07-26 17:09:05 +08:00
func (rl *RedisLock) Acquire() (bool, error) {
return rl.AcquireCtx(context.Background())
}
// AcquireCtx acquires the lock with the given ctx.
func (rl *RedisLock) AcquireCtx(ctx context.Context) (bool, error) {
2020-07-26 17:09:05 +08:00
seconds := atomic.LoadUint32(&rl.seconds)
2024-04-17 23:37:35 +08:00
resp, err := rl.store.ScriptRunCtx(ctx, lockScript, []string{rl.key}, []string{
rl.id, strconv.Itoa(int(seconds)*millisPerSecond + tolerance),
})
2024-03-08 22:35:17 +08:00
if errors.Is(err, red.Nil) {
2020-07-26 17:09:05 +08:00
return false, nil
} else if err != nil {
logx.Errorf("Error on acquiring lock for %s, %s", rl.key, err.Error())
return false, err
} else if resp == nil {
2020-07-26 17:09:05 +08:00
return false, nil
}
reply, ok := resp.(string)
if ok && reply == "OK" {
return true, nil
}
logx.Errorf("Unknown reply when acquiring lock for %s: %v", rl.key, resp)
return false, nil
2020-07-26 17:09:05 +08:00
}
// Release releases the lock.
2020-07-26 17:09:05 +08:00
func (rl *RedisLock) Release() (bool, error) {
return rl.ReleaseCtx(context.Background())
}
// ReleaseCtx releases the lock with the given ctx.
func (rl *RedisLock) ReleaseCtx(ctx context.Context) (bool, error) {
2024-04-17 23:37:35 +08:00
resp, err := rl.store.ScriptRunCtx(ctx, delScript, []string{rl.key}, []string{rl.id})
2020-07-26 17:09:05 +08:00
if err != nil {
return false, err
}
2021-02-09 13:50:21 +08:00
reply, ok := resp.(int64)
if !ok {
2020-07-26 17:09:05 +08:00
return false, nil
}
2021-02-09 13:50:21 +08:00
return reply == 1, nil
2020-07-26 17:09:05 +08:00
}
2021-12-15 13:43:05 +08:00
// SetExpire sets the expiration.
2020-07-26 17:09:05 +08:00
func (rl *RedisLock) SetExpire(seconds int) {
atomic.StoreUint32(&rl.seconds, uint32(seconds))
}