Skip to content

Commit 02e5451

Browse files
Add prefix to lock key (#134)
Co-authored-by: John Roesler <[email protected]>
1 parent aad41e4 commit 02e5451

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ func WithRedsyncOptions(options ...redsync.Option) LockerOption {
2222
locker.options = options
2323
}
2424
}
25+
26+
// WithKeyPrefix is a LockerOption for adding a prefix to the lock key
27+
func WithKeyPrefix(prefix string) LockerOption {
28+
return func(locker *redisLocker) {
29+
locker.keyPrefix = prefix
30+
}
31+
}

redislock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ type redisLocker struct {
7575
rs *redsync.Redsync
7676
options []redsync.Option
7777
autoExtendDuration time.Duration
78+
keyPrefix string
7879
}
7980

8081
func (r *redisLocker) Lock(ctx context.Context, key string) (gocron.Lock, error) {
81-
mu := r.rs.NewMutex(key, r.options...)
82+
mu := r.rs.NewMutex(r.keyPrefix+key, r.options...)
8283
err := mu.LockContext(ctx)
8384
if err != nil {
8485
return nil, ErrFailedToObtainLock

0 commit comments

Comments
 (0)