Documentation
¶
Overview ¶
Package distlock provides Redis-backed distributed locking with two usage modes: fail-hard and fail-silent.
The locker wraps the bsm/redislock library and ensures locks are always released via defer, even if the handler panics. Lock release failures are logged as warnings but do not cause errors.
Basic usage (fail-hard):
locker := distlock.New(redisClient)
err := locker.WithLock(ctx, "resource:123", 30*time.Second, func() error {
// Exclusive access to resource:123 for 30 seconds
return updateResource()
})
if err != nil && errors.Is(err, distlock.ErrLockNotObtained) {
return fmt.Errorf("failed to acquire lock")
}
For background jobs that should silently skip if the lock is unavailable:
err := locker.TryWithLock(ctx, "job:cleanup", 5*time.Minute, cleanupHandler) // ErrLockNotObtained is swallowed and logged at debug level
Lock TTL is enforced by Redis; the handler should complete well before the TTL expires.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrLockNotObtained = redislock.ErrNotObtained
Functions ¶
This section is empty.
Types ¶
type Locker ¶
type Locker struct {
// contains filtered or unexported fields
}
func New ¶
func New(redisClient redis.UniversalClient) *Locker
func (*Locker) TryWithLock ¶
Click to show internal directories.
Click to hide internal directories.