dblock

package
v0.0.0-...-7240e86 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultRefreshInterval = time.Second

DefaultRefreshInterval is the periodic duration with which a connection is refreshed/pinged

Variables

View Source
var ErrGetLockContextCancelled = errors.New("context cancelled while trying to obtain lock")

ErrGetLockContextCancelled is returned when user given context is cancelled while trying to obtain the lock

View Source
var ErrMySQLInternalError = errors.New("internal mysql error acquiring the lock")

ErrMySQLInternalError is returned when MySQL is returning a generic internal error

View Source
var ErrMySQLTimeout = errors.New("(mysql) timeout while acquiring the lock")

ErrMySQLTimeout is returned when the MySQL server can't acquire the lock in the specified timeout

Functions

func WithRefreshInterval

func WithRefreshInterval(d time.Duration) lockerOpt

WithRefreshInterval sets the duration for refresh interval for each obtained lock

Types

type Lock

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

Lock denotes an acquired lock and presents two methods, one for getting the context which is cancelled when the lock is lost/released and other for Releasing the lock

func (Lock) GetContext

func (l Lock) GetContext() context.Context

GetContext returns a context which is cancelled when the lock is lost or released

func (Lock) Release

func (l Lock) Release() error

Release unlocks the lock

type MysqlLocker

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

MysqlLocker is the client which provide APIs to obtain lock

func NewMysqlLocker

func NewMysqlLocker(db *sql.DB, lockerOpts ...lockerOpt) *MysqlLocker

NewMysqlLocker returns an instance of locker which can be used to obtain locks

func (MysqlLocker) Obtain

func (l MysqlLocker) Obtain(key string) (*Lock, error)

Obtain tries to acquire lock (with no MySQL timeout) with background context. This call is expected to block is lock is already held

func (MysqlLocker) ObtainContext

func (l MysqlLocker) ObtainContext(ctx context.Context, key string) (*Lock, error)

ObtainContext tries to acquire lock and gives up when the given context is cancelled

func (MysqlLocker) ObtainTimeout

func (l MysqlLocker) ObtainTimeout(key string, timeout int) (*Lock, error)

ObtainTimeout tries to acquire lock with background context and a MySQL timeout. This call is expected to block is lock is already held

func (MysqlLocker) ObtainTimeoutContext

func (l MysqlLocker) ObtainTimeoutContext(ctx context.Context, key string, timeout int) (*Lock, error)

ObtainTimeoutContext tries to acquire lock and gives up when the given context is cancelled

Jump to

Keyboard shortcuts

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