distrlock

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package distrlock contains DML (distributed lock manager) implementation (now DMLs based on MySQL and PostgreSQL are supported). Now only manager that uses SQL database (PostgreSQL and MySQL are currently supported) is available. Other implementations (for example, based on Redis) will probably be implemented in the future.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockAlreadyAcquired = errors.New("distributed lock already acquired")
	ErrLockAlreadyReleased = errors.New("distributed lock already released")
)

Distributed lock errors.

Functions

This section is empty.

Types

type DBLock

type DBLock struct {
	Key string
	TTL time.Duration
	// contains filtered or unexported fields
}

DBLock represents a lock object in the database.

func (*DBLock) Acquire

func (l *DBLock) Acquire(ctx context.Context, executor sqlExecutor, lockTTL time.Duration) error

Acquire acquires lock for the key in the database.

func (*DBLock) AcquireWithStaticToken

func (l *DBLock) AcquireWithStaticToken(ctx context.Context, executor sqlExecutor, token string, lockTTL time.Duration) error

AcquireWithStaticToken acquires lock for the key in the database with a static token. There two use cases for this method:

  1. When you need repeatably acquire the same lock preventing other processes from acquiring it at the same time. As an example you can block old version of workers before the upgrade and starting new version of them.
  2. When you need several processes to acquire the same lock.

Please use Acquire instead of this method unless you have a good reason to use it.

func (*DBLock) DoExclusively

func (l *DBLock) DoExclusively(
	ctx context.Context,
	dbConn *sql.DB,
	lockTTL time.Duration,
	periodicExtendInterval time.Duration,
	releaseTimeout time.Duration,
	logger log.FieldLogger,
	fn func(ctx context.Context) error,
) error

DoExclusively acquires distributed lock, starts a separate goroutine that periodical extends it and calls passed function. When function is finished, acquired lock is released.

func (*DBLock) Extend

func (l *DBLock) Extend(ctx context.Context, executor sqlExecutor) error

Extend resets expiration timeout for already acquired lock. ErrLockAlreadyReleased error will be returned if lock is already released, in this case lock should be acquired again.

func (*DBLock) Release

func (l *DBLock) Release(ctx context.Context, executor sqlExecutor) error

Release releases lock for the key in the database.

func (*DBLock) Token

func (l *DBLock) Token() string

Token returns token of the last acquired lock. May be used in logs to make investigation process easier.

type DBManager

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

DBManager provides management functionality for distributed locks based on the SQL database.

func NewDBManager

func NewDBManager(dialect dbkit.Dialect) (*DBManager, error)

NewDBManager creates new distributed lock manager that uses SQL database as a backend.

func NewDBManagerWithOpts

func NewDBManagerWithOpts(dialect dbkit.Dialect, opts DBManagerOpts) (*DBManager, error)

NewDBManagerWithOpts is a more configurable version of the NewDBManager.

func (*DBManager) Migrations

func (m *DBManager) Migrations() []migrate.Migration

Migrations returns set of migrations that must be applied before creating new locks.

func (*DBManager) NewLock

func (m *DBManager) NewLock(ctx context.Context, executor sqlExecutor, key string) (DBLock, error)

NewLock creates new initialized (but not acquired) distributed lock.

type DBManagerOpts

type DBManagerOpts struct {
	TableName string
}

DBManagerOpts represents an options for DBManager.

Jump to

Keyboard shortcuts

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