redislock

package
v0.8.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0, MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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 NewToken

func NewToken() (string, error)

NewToken returns a random owner token suitable for compare-and-* scripts.

func OwnershipContext

func OwnershipContext(ctx context.Context) context.Context

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL