Skip to content

Commit

Permalink
Fix redis lock expiration. (#655)
Browse files Browse the repository at this point in the history
Previously, expiration was set as a duration constructed from an
absolute timestamp, meaning that entries were set to expire 50+ years
in the future.
  • Loading branch information
hardlyknowem authored Feb 11, 2021
1 parent 0d5824a commit 5e7a071
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions v1/locks/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ func New(cnf *config.Config, addrs []string, db, retries int) Lock {
return lock
}

//try lock with retries
func (r Lock) LockWithRetries(key string, value int64) error {
func (r Lock) LockWithRetries(key string, unixTsToExpireNs int64) error {
for i := 0; i <= r.retries; i++ {
err := r.Lock(key, value)
err := r.Lock(key, unixTsToExpireNs)
if err == nil {
//成功拿到锁,返回
return nil
Expand All @@ -63,12 +62,12 @@ func (r Lock) LockWithRetries(key string, value int64) error {
return ErrRedisLockFailed
}

func (r Lock) Lock(key string, value int64) error {
var now = time.Now().UnixNano()

func (r Lock) Lock(key string, unixTsToExpireNs int64) error {
now := time.Now().UnixNano()
expiration := time.Duration(unixTsToExpireNs + 1 - now)
ctx := r.rclient.Context()

success, err := r.rclient.SetNX(ctx, key, value, time.Duration(value+1)).Result()
success, err := r.rclient.SetNX(ctx, key, unixTsToExpireNs, expiration).Result()
if err != nil {
return err
}
Expand All @@ -84,7 +83,7 @@ func (r Lock) Lock(key string, value int64) error {
}

if timeout != 0 && now > int64(timeout) {
newTimeout, err := r.rclient.GetSet(ctx, key, value).Result()
newTimeout, err := r.rclient.GetSet(ctx, key, unixTsToExpireNs).Result()
if err != nil {
return err
}
Expand All @@ -97,7 +96,7 @@ func (r Lock) Lock(key string, value int64) error {
if now > int64(curTimeout) {
// success to acquire lock with get set
// set the expiration of redis key
r.rclient.Expire(ctx, key, time.Duration(value+1))
r.rclient.Expire(ctx, key, expiration)
return nil
}

Expand Down

0 comments on commit 5e7a071

Please sign in to comment.