lock

package
v2.6.4 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 3 Imported by: 4

Documentation

Overview

Package lock provides a distributed lock abstraction built on a Maker/Locker pair.

A Maker creates Locker instances by name, and a Locker acquires and releases a lock. The package holds a global maker so that lockers can be created through Make without passing the maker around explicitly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close closes the global maker. It does nothing when no maker has been set.

func SetMaker

func SetMaker(maker Maker)

SetMaker sets the global locker maker. It closes the previous maker, if any, before replacing it.

Types

type Locker

type Locker interface {
	// Acquire acquires the lock.
	Acquire(ctx context.Context) error
	// TryAcquire tries to acquire the lock with an optional expiration.
	TryAcquire(ctx context.Context, expiration ...time.Duration) error
	// Release releases the lock.
	Release(ctx context.Context) error
}

Locker is a distributed lock that can be acquired and released.

func Make

func Make(name string) Locker

Make creates a locker with the given name through the global maker. It returns nil when no maker has been set.

type Maker

type Maker interface {
	// Make creates a locker with the given name.
	Make(name string) Locker
	// Close closes the maker.
	Close() error
}

Maker creates and closes lockers.

func GetMaker

func GetMaker() Maker

GetMaker returns the global locker maker.

type Option

type Option struct {
	Once       bool          // Once reports whether the lock is acquired only once; by default it is acquired in blocking mode until it succeeds.
	Expiration time.Duration // Expiration is the lock expiration time.
}

Option configures how a lock is acquired.

Jump to

Keyboard shortcuts

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