Help us shape the future of Go! Take the 2026 Go Developer Survey

lock

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DistributedLock

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

DistributedLock provides distributed locking using Redis.

func NewDistributedLock

func NewDistributedLock(client *redis.Client, key string, ttl time.Duration) *DistributedLock

NewDistributedLock creates a new distributed lock.

func (*DistributedLock) Acquire

func (l *DistributedLock) Acquire(ctx context.Context) error

Acquire blocks until the lock is acquired or context is canceled.

func (*DistributedLock) Extend

func (l *DistributedLock) Extend(ctx context.Context) error

Extend extends the lock TTL if we still own it.

func (*DistributedLock) IsHeld

func (l *DistributedLock) IsHeld(ctx context.Context) (bool, error)

IsHeld checks if we currently hold the lock.

func (*DistributedLock) Release

func (l *DistributedLock) Release(ctx context.Context) error

Release releases the lock if we own it Uses Lua script for atomic check-and-delete.

func (*DistributedLock) TryAcquire

func (l *DistributedLock) TryAcquire(ctx context.Context) (bool, error)

TryAcquire attempts to acquire the lock without blocking Returns true if lock was acquired, false otherwise.

func (*DistributedLock) TryWithLock

func (l *DistributedLock) TryWithLock(
	ctx context.Context,
	fn func(ctx context.Context) error,
) (bool, error)

TryWithLock executes a function if the lock can be acquired Returns false if lock was not acquired.

func (*DistributedLock) WithLock

func (l *DistributedLock) WithLock(ctx context.Context, fn func(ctx context.Context) error) error

WithLock executes a function while holding the lock Automatically acquires before and releases after.

Jump to

Keyboard shortcuts

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