redis-lock

func (r *RedisClient) Lock(key string, expiration time.Duration) (*lock.Locker, error) {
	locker, err := lock.Obtain(r.Client, key, &lock.Options{
		LockTimeout: expiration,
		RetryCount:  2,
	})
	if err != nil {
		logrus.WithField("model", "redis").Errorf("get redis lock err, %s", err.Error())
		return nil, errors.New("get redis lock err")
	} else if locker == nil {
		logrus.WithField("model", "redis").Errorf("could not obtain lock, %s", err.Error())
		return nil, errors.New("could not obtain lock")
	}
	return locker, nil
}

bsm/redis-lock: https://github.com/bsm/redis-lock

你可能感兴趣的:(go)