Documentation
¶
Overview ¶
Package redislock provides token-owned Redis locks with atomic renewal and release. Callers must treat loss of ownership as loss of exclusive access.
Index ¶
- Variables
- func Acquire(ctx context.Context, client redis.UniversalClient, key string, token string, ...) error
- func NewToken() (string, error)
- func OwnershipContext(ctx context.Context) context.Context
- func Release(ctx context.Context, client redis.UniversalClient, key string, token string) (bool, error)
- func Renew(ctx context.Context, client redis.UniversalClient, key string, token string, ...) (bool, error)
- func TryAcquire(ctx context.Context, client redis.UniversalClient, key string, token string, ...) (bool, error)
- func WithRenewableLock(ctx context.Context, client redis.UniversalClient, key string, ...) (resultErr error)
Constants ¶
This section is empty.
Variables ¶
var ErrLockOwnershipLost = errors.New("redis lock ownership lost")
ErrLockOwnershipLost means exclusive ownership can no longer be guaranteed.
Functions ¶
func Acquire ¶
func Acquire( ctx context.Context, client redis.UniversalClient, key string, token string, lease time.Duration, ) error
Acquire waits until the lock is acquired or ctx is cancelled.
func OwnershipContext ¶
OwnershipContext returns a context that is canceled only when lock ownership is lost, not when the request context is canceled. Outside a renewable-lock callback it falls back to a cancellation-detached context.
func Release ¶
func Release( ctx context.Context, client redis.UniversalClient, key string, token string, ) (bool, error)
Release deletes the lock only if token still owns it.
func Renew ¶
func Renew( ctx context.Context, client redis.UniversalClient, key string, token string, lease time.Duration, ) (bool, error)
Renew extends the lease only if token still owns the lock.
func TryAcquire ¶
func TryAcquire( ctx context.Context, client redis.UniversalClient, key string, token string, lease time.Duration, ) (bool, error)
TryAcquire attempts one SET NX acquisition.
func WithRenewableLock ¶
func WithRenewableLock( ctx context.Context, client redis.UniversalClient, key string, lease time.Duration, renewInterval time.Duration, fn func(context.Context) error, ) (resultErr error)
WithRenewableLock acquires key, renews it while fn runs, and atomically releases it on exit. Renewal failure cancels the context passed to fn.
Types ¶
This section is empty.