Skip to content

Commit

Permalink
Add redsync Mutex options for LockRedis and use context in LockRedis
Browse files Browse the repository at this point in the history
  • Loading branch information
GRbit committed Jan 19, 2024
1 parent b9fb5f6 commit f9da9b2
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,21 @@ type LockRedis struct {
}

// NewLockRedis creates a new instance of LockRedis.
func NewLockRedis(pool redsyncredis.Pool, mutexName string) *LockRedis {
func NewLockRedis(pool redsyncredis.Pool, mutexName string, options ...redsync.Option) *LockRedis {
rs := redsync.New(pool)
mutex := rs.NewMutex(mutexName)
mutex := rs.NewMutex(mutexName, options...)
return &LockRedis{mutex: mutex}
}

// Lock locks the lock in Redis.
func (l *LockRedis) Lock(_ context.Context) error {
err := l.mutex.Lock()
func (l *LockRedis) Lock(ctx context.Context) error {
err := l.mutex.LockContext(ctx)
return errors.Wrap(err, "failed to lock a mutex in redis")
}

// Unlock unlocks the lock in Redis.
func (l *LockRedis) Unlock(_ context.Context) error {
if ok, err := l.mutex.Unlock(); !ok || err != nil {
func (l *LockRedis) Unlock(ctx context.Context) error {
if ok, err := l.mutex.UnlockContext(ctx); !ok || err != nil {
return errors.Wrap(err, "failed to unlock a mutex in redis")
}
return nil
Expand Down

0 comments on commit f9da9b2

Please sign in to comment.