2020-07-26 17:09:05 +08:00
|
|
|
package redis
|
|
|
|
|
|
|
|
|
|
import (
|
2022-07-30 18:38:36 +08:00
|
|
|
"context"
|
2024-04-17 23:20:10 +08:00
|
|
|
_ "embed"
|
2024-03-08 22:35:17 +08:00
|
|
|
"errors"
|
2020-07-26 17:09:05 +08:00
|
|
|
"math/rand"
|
2022-03-24 16:17:01 +08:00
|
|
|
"strconv"
|
2020-07-26 17:09:05 +08:00
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
|
|
|
|
|
2024-01-13 22:40:58 +08:00
|
|
|
red "github.com/redis/go-redis/v9"
|
2022-01-04 15:51:32 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2022-03-24 16:17:01 +08:00
|
|
|
randomLen = 16
|
|
|
|
|
tolerance = 500 // milliseconds
|
|
|
|
|
millisPerSecond = 1000
|
2023-04-02 11:28:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2024-04-17 23:20:10 +08:00
|
|
|
//go:embed lockscript.lua
|
2024-04-17 23:37:35 +08:00
|
|
|
lockLuaScript string
|
|
|
|
|
lockScript = NewScript(lockLuaScript)
|
2024-04-17 23:20:10 +08:00
|
|
|
|
|
|
|
|
//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
|
|
|
)
|
|
|
|
|
|
2021-02-28 23:02:49 +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
|
|
|
}
|
|
|
|
|
|
2021-02-28 23:02:49 +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,
|
2021-11-10 21:14:21 +08:00
|
|
|
id: stringx.Randn(randomLen),
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 23:02:49 +08:00
|
|
|
// Acquire acquires the lock.
|
2020-07-26 17:09:05 +08:00
|
|
|
func (rl *RedisLock) Acquire() (bool, error) {
|
2022-07-30 19:46:10 +08:00
|
|
|
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{
|
2022-03-24 16:17:01 +08:00
|
|
|
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
|
2022-03-24 16:17:01 +08:00
|
|
|
} else if resp == nil {
|
2020-07-26 17:09:05 +08:00
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 16:17:01 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-02-28 23:02:49 +08:00
|
|
|
// Release releases the lock.
|
2020-07-26 17:09:05 +08:00
|
|
|
func (rl *RedisLock) Release() (bool, error) {
|
2022-07-30 19:46:10 +08:00
|
|
|
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))
|
|
|
|
|
}
|