driver

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Max length of a named mutex.
	MaxMutexNameLength = 256
	// Max length of hostname.
	MaxHostnameLength = 256
	// Max length of locker id.
	MaxLockerIdLength = 256
)

These constants hold maximum lengths for a few attributes.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

type Driver interface {
	// CreateLockTableIfNotExists creates tableName if it does not exists.
	CreateMutexTableIfNotExists(
		ctx context.Context,
		ex Execer,
		tableName string,
	) error

	// CreateLockEntryIfNotExists creates an entry in tableName for mutexName.
	CreateMutexEntryIfNotExists(
		ctx context.Context,
		qe QueryExecer,
		tableName string,
		mutexName string,
	) error

	// AcquireLock attempts to acquire the mutex identified by tableName and mutexName. It returns true
	// if the mutex was acquired. hostname, pid and lockerId are stored with the mutex in order to help
	// with lock holder identification.  The mutex should be refreshed (by calling Refresh) after
	// refresh time elapses and will expire after expires time.
	Lock(
		ctx context.Context,
		ex Execer,
		tableName string,
		mutexName string,
		hostname string,
		pid int,
		lockerId string,
		refresh time.Duration,
		expires time.Duration,
	) (bool, error)

	// Refresh refreshes a locked mutex by updating the row in tableName for the mutex identified by
	// mutexName, hostname, pid and lockerId.  Refresh should be called again in refresh time.
	// Return true if the lock was refreshed successfully.
	Refresh(
		ctx context.Context,
		ex Execer,
		tableName string,
		mutexName string,
		hostname string,
		pid int,
		lockerId string,
		refresh time.Duration,
		expires time.Duration,
	) (bool, error)

	// ReleaseLock releases a locked mutex by updating the row in tableName for the mutex identified by
	// mutexName, hostname, pid and lockerId. Return true if the mutex was unlocked successfully.
	Unlock(
		ctx context.Context,
		ex Execer,
		tableName string,
		mutexName string,
		hostname string,
		pid int,
		lockerId string,
	) (bool, error)
}

Driver is the interface that must be implemented by the database-based locker. All operations should fail fast (returning an error) since retry logic is handled outside of Driver.

func ResolveDriver

func ResolveDriver(ctx context.Context, db *sql.DB) (Driver, error)

ResolveDriver attempts to inspect db and determine which Driver should be used. If able to detect the Driver, it is returned.

type Execer

type Execer interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

Execer is the contract used when executing statements against the DB.

type MysqlDriver

type MysqlDriver struct{}

MysqlDriver is the mysql specific Driver implementation.

func (MysqlDriver) CreateMutexEntryIfNotExists

func (MysqlDriver) CreateMutexEntryIfNotExists(
	ctx context.Context,
	qe QueryExecer,
	tableName string,
	mutexName string,
) error

func (MysqlDriver) CreateMutexTableIfNotExists

func (MysqlDriver) CreateMutexTableIfNotExists(
	ctx context.Context,
	ex Execer,
	tableName string,
) error

func (MysqlDriver) Lock

func (MysqlDriver) Lock(
	ctx context.Context,
	ex Execer,
	tableName string,
	mutexName string,
	hostname string,
	pid int,
	lockerId string,
	refresh time.Duration,
	expires time.Duration,
) (bool, error)

func (MysqlDriver) Refresh

func (MysqlDriver) Refresh(
	ctx context.Context,
	ex Execer,
	tableName string,
	mutexName string,
	hostname string,
	pid int,
	lockerId string,
	refresh time.Duration,
	expires time.Duration,
) (bool, error)

func (MysqlDriver) Unlock

func (MysqlDriver) Unlock(
	ctx context.Context,
	ex Execer,
	tableName string,
	mutexName string,
	hostname string,
	pid int,
	lockerId string,
) (bool, error)

type PostgresDriver

type PostgresDriver struct{}

PostgresDriver is the postgres specific Driver implementation.

func (PostgresDriver) CreateMutexEntryIfNotExists

func (PostgresDriver) CreateMutexEntryIfNotExists(
	ctx context.Context,
	qe QueryExecer,
	tableName string,
	mutexName string,
) error

func (PostgresDriver) CreateMutexTableIfNotExists

func (PostgresDriver) CreateMutexTableIfNotExists(
	ctx context.Context,
	ex Execer,
	tableName string,
) error

func (PostgresDriver) Lock

func (PostgresDriver) Lock(
	ctx context.Context,
	ex Execer,
	tableName string,
	mutexName string,
	hostname string,
	pid int,
	lockerId string,
	refresh time.Duration,
	expires time.Duration,
) (bool, error)

func (PostgresDriver) Refresh

func (PostgresDriver) Refresh(
	ctx context.Context,
	ex Execer,
	tableName string,
	mutexName string,
	hostname string,
	pid int,
	lockerId string,
	refresh time.Duration,
	expires time.Duration,
) (bool, error)

func (PostgresDriver) Unlock

func (PostgresDriver) Unlock(
	ctx context.Context,
	ex Execer,
	tableName string,
	mutexName string,
	hostname string,
	pid int,
	lockerId string,
) (bool, error)

type QueryExecer

type QueryExecer interface {
	Execer
	Queryer
}

QueryExecer is both an Execer and a Queryer.

type Queryer

type Queryer interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

Execer is the contract used when querying against the DB.

Jump to

Keyboard shortcuts

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