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 ¶
- Variables
- type DBLock
- func (l *DBLock) Acquire(ctx context.Context, executor sqlExecutor, lockTTL time.Duration) error
- func (l *DBLock) AcquireWithStaticToken(ctx context.Context, executor sqlExecutor, token string, lockTTL time.Duration) error
- func (l *DBLock) DoExclusively(ctx context.Context, dbConn *sql.DB, lockTTL time.Duration, ...) error
- func (l *DBLock) Extend(ctx context.Context, executor sqlExecutor) error
- func (l *DBLock) Release(ctx context.Context, executor sqlExecutor) error
- func (l *DBLock) Token() string
- type DBManager
- type DBManagerOpts
Constants ¶
This section is empty.
Variables ¶
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 ¶
DBLock represents a lock object 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:
- 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.
- 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 ¶
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.
type DBManager ¶
type DBManager struct {
// contains filtered or unexported fields
}
DBManager provides management functionality for distributed locks based on the SQL database.
func NewDBManager ¶
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 ¶
Migrations returns set of migrations that must be applied before creating new locks.
type DBManagerOpts ¶
type DBManagerOpts struct {
TableName string
}
DBManagerOpts represents an options for DBManager.