lock

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultName          = "default"
	Local                = "local"
	Schedule             = "schedule"
	Queue                = "queue"
	Cache                = "cache"
	DefaultDriver        = "memory"
	DefaultTTL           = 30 * time.Second
	DefaultWait          = 3 * time.Second
	DefaultRetryInterval = 100 * time.Millisecond
	DefaultRedisPrefix   = "runa:lock:"
)

Variables

View Source
var (
	ErrTimeout = errors.New("lock wait timeout")
	ErrNotHeld = errors.New("lock is not held")
)

Functions

func AutoRenew

func AutoRenew(enabled bool) autoRenewOption

AutoRenew enables or disables automatic lease renew in With.

func Meta

func Meta(key string, value any) metaOption

Meta sets locker metadata.

func Prefix

func Prefix(value string) prefixOption

Prefix sets a key prefix for a locker or store.

func Provider

func Provider(options ...ProviderOption) runaprovider.Provider

func RetryInterval

func RetryInterval(duration time.Duration) retryIntervalOption

RetryInterval sets polling interval while waiting.

func TTL

func TTL(duration time.Duration) ttlOption

TTL sets lock ttl.

func Wait

func Wait(duration time.Duration) waitOption

Wait sets max blocking wait duration.

Types

type Driver

type Driver interface {
	Name() string
	Try(ctx context.Context, key string, token string, ttl time.Duration) (State, bool, error)
	Renew(ctx context.Context, key string, token string, ttl time.Duration) error
	Release(ctx context.Context, key string, token string) error
	Close(ctx context.Context) error
}

Driver is the primitive lock storage contract.

func MemoryDriver

func MemoryDriver(options ...DriverOption) Driver

MemoryDriver creates a single-process lock driver.

type DriverOption

type DriverOption interface {
	ApplyDriver(*DriverOptions)
}

DriverOption configures a lock driver.

func DriverMeta

func DriverMeta(key string, value any) DriverOption

DriverMeta sets lock driver metadata.

type DriverOptions

type DriverOptions struct {
	Name   string
	Prefix string
	Meta   core.Map
}

DriverOptions stores backend lock driver settings.

type Info

type Info struct {
	Name          string
	Driver        string
	Prefix        string
	TTL           time.Duration
	Wait          time.Duration
	RetryInterval time.Duration
	AutoRenew     bool
	Meta          core.Map
}

Info describes a configured lock pool.

type Lease

type Lease interface {
	Key() string
	Token() string
	Fencing() uint64
	Renew(ctx context.Context, ttl time.Duration) error
	Release(ctx context.Context) error
}

Lease is an acquired lock handle.

type LockOption

type LockOption interface {
	ApplyLock(*Options)
}

LockOption configures one lock acquisition.

type Locker

type Locker interface {
	Try(ctx context.Context, key string, options ...LockOption) (Lease, bool, error)
	Wait(ctx context.Context, key string, options ...LockOption) (Lease, error)
	With(ctx context.Context, key string, fn func(context.Context) error, options ...LockOption) error
}

Locker is a named lock pool.

type LockerOption

type LockerOption interface {
	ApplyLocker(*Options)
}

LockerOption configures a named locker.

func Use

func Use(name string) LockerOption

Driver selects the backing lock driver used by a locker.

type Options

type Options struct {
	Driver        string
	Prefix        string
	TTL           time.Duration
	Wait          time.Duration
	RetryInterval time.Duration
	AutoRenew     bool
	Meta          core.Map
}

Options stores locker and lock-call settings.

type ProviderOption

type ProviderOption func(*provider)

func RegisterDriver

func RegisterDriver(name string, driver Driver) ProviderOption

func RegisterLocker

func RegisterLocker(name string, options ...LockerOption) ProviderOption

type Registry

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

Registry stores lock drivers and named lockers.

func Default

func Default() *Registry

Default returns the default app lock registry.

func New

func New() *Registry

New creates a lock registry with the memory driver installed.

func (*Registry) Close

func (registry *Registry) Close(ctx context.Context) error

Close closes all registered drivers.

func (*Registry) Config

func (registry *Registry) Config(store *config.Store)

Config applies file/env config to already registered lockers.

func (*Registry) Driver

func (registry *Registry) Driver(name string) Driver

Driver returns a registered driver by name.

func (*Registry) Info

func (registry *Registry) Info() []Info

Info returns configured locker snapshots.

func (*Registry) Locker

func (registry *Registry) Locker(name string, options ...LockerOption)

Locker registers a named locker.

func (*Registry) MustOf

func (registry *Registry) MustOf(name string) Locker

MustOf returns a named locker or panics.

func (*Registry) Of

func (registry *Registry) Of(name string) (Locker, error)

Of returns a named locker.

func (*Registry) RegisterDriver

func (registry *Registry) RegisterDriver(name string, driver Driver)

RegisterDriver registers a lock driver.

func (*Registry) Shutdown

func (registry *Registry) Shutdown(ctx context.Context) error

Shutdown closes all lock drivers when managed by DI.

type State

type State struct {
	Key       string
	Token     string
	Fencing   uint64
	ExpiresAt time.Time
}

State is the store-level lock state.

Jump to

Keyboard shortcuts

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