locker

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 8 Imported by: 0

README

Locker Package

The locker package provides distributed locking mechanisms for concurrent resource access control.

Features

  • Distributed Locking: Prevent concurrent access to shared resources
  • Multiple Implementations: Local and Redis-based lockers
  • Timeout Support: Lock with timeout to prevent deadlocks
  • Wait for Locker: Block until lock is available

Main Components

Locker Interface

Core locking interface:

  • Lock(ctx, resource): Acquire a lock immediately
  • LockWithTimeout(ctx, resource, timeout): Acquire lock with timeout
  • WaitForLocker(ctx, resource, maxWait, timeout): Wait for lock availability
Release

Function type for releasing locks:

  • Returns error if release fails
  • Should be called in defer statement

Implementations

  • Local: In-memory locking for single-instance applications
  • Redis: Distributed locking for multi-instance deployments

Usage

// Acquire lock
release, err := locker.Lock(ctx, "resource-key")
if err != nil {
    return err
}
defer release(ctx)

// Critical section
// ...

Dependencies

  • Context for cancellation
  • Redis (for distributed locking)

Documentation

Index

Constants

View Source
const (
	LockerPrefix = "_locker_"
)

Variables

View Source
var ErrLocked = errors.New("locker: resource locked")
View Source
var MaxLockerDuration time.Duration = 3 * time.Minute
View Source
var WaitInteval time.Duration = 50 * time.Millisecond

Functions

This section is empty.

Types

type Locker

type Locker interface {
	Lock(ctx context.Context, resource string) (Release, error)
	WaitForLocker(ctx context.Context, resource string, maxWait time.Duration, timeout time.Duration) (Release, error)
	LockWithtimeout(ctx context.Context, resource string, timeout time.Duration) (Release, error)
}

func InitRedisLocker added in v0.9.0

func InitRedisLocker(logger *zap.Logger, client *redis.Client) Locker

type RedisLocker added in v0.9.0

type RedisLocker struct {
	Logger *zap.Logger
	// contains filtered or unexported fields
}

func (*RedisLocker) Lock added in v0.9.0

func (r *RedisLocker) Lock(ctx context.Context, resource string) (Release, error)

func (*RedisLocker) LockWithtimeout added in v0.9.0

func (r *RedisLocker) LockWithtimeout(ctx context.Context, resource string, timeout time.Duration) (Release, error)

func (*RedisLocker) WaitForLocker added in v0.10.7

func (r *RedisLocker) WaitForLocker(ctx context.Context, resource string, maxWait time.Duration, timeout time.Duration) (Release, error)
func (r *RedisLocker) Init() {
	c := redis.NewClient(r.RedisOptions)
	r.client = c
	r.Logger.Info("redis locker ready.", zap.String("redis", r.RedisOptions.Addr))
}

type Release

type Release func(context.Context) error

Jump to

Keyboard shortcuts

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