redsync

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedsyncLockManager

func NewRedsyncLockManager(redisClient redis.UniversalClient, opts ...Option) lockmanager.LockManager

NewRedsyncLockManager constructs a new distributed LockManager using go-redis as backend. Optional configuration such as custom token generator and default mutex options can be provided.

Example usage:

locker := redsync.NewRedsyncLockManager(
    redisClient,
    redsync.WithTokenGenerator(func(key string) string {
        return "custom-token-for:" + key
    }),
    redsync.WithRedsyncOptions(
        redsync.WithTries(10),
        redsync.WithRetryDelay(100*time.Millisecond),
    ),
)

token, err := locker.Acquire(ctx, "resource-key", 5*time.Second)
if err != nil {
    log.Fatalf("failed to acquire lock: %v", err)
}

defer locker.Release(ctx, "resource-key", token)

Types

type Option

type Option func(*redsyncLockManager)

Option represents a functional option for configuring redsyncLockManager.

func WithRedsyncOptions

func WithRedsyncOptions(opts ...redsync.Option) Option

WithRedsyncOptions sets default options for all mutexes created by this LockManager. These options apply to both Acquire and Release calls.

func WithTokenGenerator

func WithTokenGenerator(f func(key string) string) Option

WithTokenGenerator sets a custom function for generating lock tokens based on the lock key. If not provided, a default XID-based generator will be used.

Jump to

Keyboard shortcuts

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