leases

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package leases provides an API for acquiring and releasing leases.

Index

Constants

This section is empty.

Variables

View Source
var ErrLeaseHeld = errors.New("lease is held")

ErrLeaseHeld is returned when the lease acquisition times out because the lease is already held.

View Source
var ErrLeaseNotHeld = errors.New("lease is not held")

ErrLeaseNotHeld is returned by Release when the lease is not held.

Functions

func SQLLeaserMigrations

func SQLLeaserMigrations() zerosql.Migrations

Types

type Leaser

type Leaser interface {
	// Acquire acquires a lease for the given key.
	//
	// It will block until the lease is acquired, timeout is reached, or context is cancelled. Once acquired, the lease
	// will be automatically released when the context is canceled, or when the Release function is called.
	//
	// Leases are automatically renewed, but if renewal fails after a period of retries, the process will be terminated
	// to avoid split-brain.
	Acquire(ctx context.Context, key string, timeout time.Duration) (Release, error)

	// Close releases all resources associated with the leaser.
	Close() error
}

Leaser is an interface for lease acquisition and release.

func NewMemoryLeaser

func NewMemoryLeaser() Leaser

NewMemoryLeaser creates a Leaser that holds leases using an in-memory map.

On the upside, it can never fail.

func NewSQLLeaser

func NewSQLLeaser(
	ctx context.Context,
	logger *slog.Logger,
	driver zerosql.Driver,
	db *sql.DB,
) (Leaser, error)

NewSQLLeaser creates a Leaser backed by an SQL database.

type MemoryLeaser

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

func (*MemoryLeaser) Acquire

func (m *MemoryLeaser) Acquire(ctx context.Context, key string, timeout time.Duration) (Release, error)

func (*MemoryLeaser) Close

func (m *MemoryLeaser) Close() error

type Release

type Release func(ctx context.Context) error

Release an acquired lease.

type SQLLeaser

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

func (*SQLLeaser) Acquire

func (s *SQLLeaser) Acquire(ctx context.Context, key string, timeout time.Duration) (Release, error)

func (*SQLLeaser) Close

func (s *SQLLeaser) Close() error

Jump to

Keyboard shortcuts

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