You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi I have 2 helper methods that I'm using to acquire and release the mutex
func (c RedisCache) AcquireLockWithExpiry(ctx context.Context, mutexId string, ttl time.Duration) error {
redisClient, err := c.redisClientProvider.Get(ctx, c.redisConfig)
if err != nil {
return errors.Join(err, errors.New("could not get redis client"))
}
pool := goredis.NewPool(redisClient)
rs := redsync.New(pool)
mutex := rs.NewMutex(mutexId, redsync.WithExpiry(ttl), redsync.WithValue(mutexId))
c.logger.Infow(ctx, "acquiring lock in redis cache", "mutexId", mutexId, "mutexValue", mutex.Value())
if err := mutex.LockContext(ctx); err != nil {
c.logger.Warnw(ctx, "failed to acquire lock in redis cache", "err", err, "mutexId", mutexId)
return errors.Join(err, internalErrors.MutexAlreadyExists)
}
return nil
}
func (c RedisCache) ReleaseLock(ctx context.Context, mutexId string) (bool, error) {
redisClient, err := c.redisClientProvider.Get(ctx, c.redisConfig)
if err != nil {
return false, errors.Join(err, errors.New("could not get redis client"))
}
pool := goredis.NewPool(redisClient)
rs := redsync.New(pool)
mutex := rs.NewMutex(mutexId, redsync.WithValue(mutexId))
c.logger.Infow(ctx, "releasing lock in redis cache", "mutexId", mutexId, "mutexValue", mutex.Value())
if _, unlockErr := mutex.Unlock(); unlockErr != nil {
c.logger.Errorw(ctx, "failed to release lock in redis cache", "unlockErr", unlockErr, "mutexId", mutexId)
return false, errors.Join(err, internalErrors.MutexAlreadyExists)
}
return true, nil
}
I have an async workflow where first I call the AcquireLockWithExpiry to lock the mutex. Then I perform some business logic and call the ReleaseLock function to unlock. I use the WithValue option to use a pre-determined value that I use while locking and unlocking. Unfortunately while unlocking I get the error lock already taken, locked nodes: [0]. How do I fix this? Is there any recommendation around this? Am I supposed to use the same pool to create the mutex for locking and unlocking?
The text was updated successfully, but these errors were encountered:
Hi I have 2 helper methods that I'm using to acquire and release the mutex
I have an async workflow where first I call the AcquireLockWithExpiry to lock the mutex. Then I perform some business logic and call the ReleaseLock function to unlock. I use the WithValue option to use a pre-determined value that I use while locking and unlocking. Unfortunately while unlocking I get the error
lock already taken, locked nodes: [0]
. How do I fix this? Is there any recommendation around this? Am I supposed to use the same pool to create the mutex for locking and unlocking?The text was updated successfully, but these errors were encountered: