locker

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package locker provides a mechanism for fine-grained locking.

In contrast to a sync.Mutex, the user must provide a key when locking and unlocking.

If a lock with a given key does not exist when Lock() is called, one is created. Lock references are automatically cleaned up if nothing is waiting for the lock anymore.

Locking can be aborted on context cancellation.

This package is inspired by https://github.com/moby/locker, but uses a buffered channel instead of a sync.Mutex to allow for context cancellation. Even though the standard library does not offer to cancel locking a mutex, golang.org/x/sync/semaphore does for its Acquire() method.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

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

Locker offers thread-safe locking of entities represented by keys of type string.

func New

func New() *Locker

New returns a new Locker.

func (*Locker) Lock

func (l *Locker) Lock(ctx context.Context, key string) error

Lock locks the given key. Returns after acquiring lock or when given context is done. The only errors it can return stem from the context being done.

func (*Locker) Unlock

func (l *Locker) Unlock(key string)

Unlock unlocks the given key. It panics if the key is not locked.

Jump to

Keyboard shortcuts

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