lock

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package lock implements named distributed locks with fencing tokens.

Every successful acquisition gets a token from one server-wide counter that only goes up. The counter is seeded from the clock (unix microseconds), so tokens keep increasing across restarts as long as the clock doesn't go back. Locks are leader-local: they are neither persisted nor replicated.

Expiry is lazy (checked on every access) plus a timer per lock, which removes it and wakes waiters when it expires.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	Token int64
	Owner string
	TTL   time.Duration
}

Info describes a held lock.

type Manager

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

func New

func New() *Manager

func (*Manager) Acquire

func (m *Manager) Acquire(name string, ttl time.Duration, owner string, wait time.Duration, cancel ...<-chan struct{}) (int64, bool)

Acquire takes the lock, waiting up to wait for it to be freed. It returns early (false) when cancel is closed.

func (*Manager) Extend

func (m *Manager) Extend(name string, token int64, ttl time.Duration) bool

Extend sets a new ttl (from now) if token matches the live holder.

func (*Manager) Get

func (m *Manager) Get(name string) (Info, bool)

Get returns the live holder of name.

func (*Manager) Len

func (m *Manager) Len() int

Len returns the number of held locks.

func (*Manager) Release

func (m *Manager) Release(name string, token int64) bool

Release frees the lock if token matches the live holder.

func (*Manager) TryAcquire

func (m *Manager) TryAcquire(name string, ttl time.Duration, owner string) (int64, bool)

TryAcquire takes the lock if it is free (or expired).

Jump to

Keyboard shortcuts

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