Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redis.Lock bug #780

Open
huyidaoxinyu opened this issue Aug 2, 2022 · 0 comments
Open

redis.Lock bug #780

huyidaoxinyu opened this issue Aug 2, 2022 · 0 comments

Comments

@huyidaoxinyu
Copy link

huyidaoxinyu commented Aug 2, 2022

I found a bug in the function of struct redis.Lock in v2/locks/redis/redis.go file.
if the key not expired when r.rclient.Get,but expired when r.rclient.GetSet, it will lock failed.
My fix codes like this:

 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, unixTsToExpireNs, expiration).Result()
	if err != nil {
		return err
	}

	if !success {
		v, err := r.rclient.Get(ctx, key).Result()
		if err != nil {
			return err
		}
		timeout, err := strconv.Atoi(v)
		if err != nil {
			return err
		}

		if timeout != 0 && now > int64(timeout) {
			newTimeout, err := r.rclient.GetSet(ctx, key, unixTsToExpireNs).Result()
			if err != nil&& err != redis.Nil {
				return err
			}

			curTimeout := 0
			if newTimeout != "" {
				curTimeout, err := strconv.Atoi(newTimeout)
				if err != nil {
					return err
				}
			}

			if now > int64(curTimeout) {
				// success to acquire lock with get set
				// set the expiration of redis key
				r.rclient.Expire(ctx, key, expiration)
				return nil
			}

			return ErrRedisLockFailed
		}

		return ErrRedisLockFailed
	}

	return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant