lock

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InMemoryLocker added in v0.5.0

type InMemoryLocker struct {
	// contains filtered or unexported fields
}

InMemoryLocker implements Locker using in-process mutexes.

func NewInMemoryLocker added in v0.5.0

func NewInMemoryLocker() *InMemoryLocker

NewInMemoryLocker creates a new in-process Locker.

func (*InMemoryLocker) Acquire added in v0.5.0

func (l *InMemoryLocker) Acquire(ctx context.Context, key string, ttl time.Duration) error

func (*InMemoryLocker) Release added in v0.5.0

func (l *InMemoryLocker) Release(_ context.Context, key string) error

func (*InMemoryLocker) TryLock added in v0.5.0

func (l *InMemoryLocker) TryLock(_ context.Context, key string, _ time.Duration) (bool, error)

type Locker

type Locker interface {
	// Acquire blocks until the lock for key is obtained or the context is cancelled.
	// If ttl is greater than zero, the lock is automatically released after the duration.
	Acquire(ctx context.Context, key string, ttl time.Duration) error

	// TryLock attempts to obtain the lock without waiting. It returns true on success.
	TryLock(ctx context.Context, key string, ttl time.Duration) (bool, error)

	// Release frees the lock for the given key.
	Release(ctx context.Context, key string) error
}

Locker defines the interface for distributed or local locks.

Example:

// Assuming a RedisLocker implementation
locker := NewRedisLocker(redisClient)
locker.Acquire(ctx, "my-resource", 5*time.Second)

Jump to

Keyboard shortcuts

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