redsync

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 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. The lock manager provides distributed mutex functionality across multiple Redis instances with configurable retry behavior and token generation.

Parameters:

  • redisClient: Redis client (single instance, cluster, or sentinel)
  • opts: Optional configuration for token generation and mutex behavior

Returns:

  • lockmanager.LockManager: Configured distributed lock manager

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, enabling consistent behavior across all locks managed by this instance.

Parameters:

  • opts: Redsync options to apply to all mutexes

Returns:

  • Option: Configuration option for the lock manager

Example:

WithRedsyncOptions(
    redsync.WithTries(5),
    redsync.WithRetryDelay(200*time.Millisecond),
)

func WithTokenGenerator

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

WithTokenGenerator sets a custom function for generating lock tokens based on the lock key. Tokens are used to identify lock ownership and ensure only the lock holder can release it. If not provided, a default XID-based generator will be used.

Parameters:

  • f: Function that takes a lock key and returns a unique token

Returns:

  • Option: Configuration option for the lock manager

Example:

WithTokenGenerator(func(key string) string {
    return fmt.Sprintf("service-a-%s-%d", key, time.Now().Unix())
})

Jump to

Keyboard shortcuts

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