lock

package
v0.0.0-...-97dcd52 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2018 License: AGPL-3.0 Imports: 5 Imported by: 1

Documentation

Overview

Package lock provides a mechanism for distributed locks. Several instances of components (same component name, different component IDs) can compete to execute Jobs. We want that a given job (recognised by its job name) is executed only once at a time. To achive that we use CockroachDB, a distributed ACID DB. Each component will execute a transaction to lock the job, the transaction that succeed will obtain the lock, and the component can execute the corresponding job. Also, when a lock is disabled, the Lock method will always return an ErrDisabled error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrDisabled

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

ErrDisabled is the error returned when the lock is disabled.

func (*ErrDisabled) Error

func (e *ErrDisabled) Error() string

type ErrUnauthorised

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

ErrUnauthorised is the error returned when there is a call to the Lock method, but there is already another component owning the lock.

func (*ErrUnauthorised) Error

func (e *ErrUnauthorised) Error() string

type LocalLock

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

LocalLock is the local lock

func NewLocalLock

func NewLocalLock() *LocalLock

NewLocalLock returns a lock local, that is a lock that does not use the distributed Storage to assure that only one instance of the job is running in the cluster.

func (*LocalLock) Lock

func (l *LocalLock) Lock() error

Lock locks locally.

func (*LocalLock) Unlock

func (l *LocalLock) Unlock() error

Unlock unlocks locally.

type Lock

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

Lock is the locking module.

func New

func New(s Storage) *Lock

New returns a new locking module.

func (*Lock) Disable

func (l *Lock) Disable(componentName, jobName string) error

Disable disable the job. It sets the 'enabled' boolean to false for the current job.

func (*Lock) Enable

func (l *Lock) Enable(componentName, jobName string) error

Enable enable the job. It sets the 'enabled' boolean to true for the current job.

func (*Lock) Lock

func (l *Lock) Lock(componentName, componentID, jobName, jobID string, jobMaxDuration time.Duration) error

Lock try to reserve and lock the job in the distributed DB. It returns a nil error if the reservation succeeded, and an error if it didn't.

func (*Lock) Unlock

func (l *Lock) Unlock(componentName, componentID, jobName, jobID string) error

Unlock unlocks the lock in the distributed DB. A job can be unlocked it it is disabled. Unlock returns a nil error if the operation succeeded, and an error if it didn't.

type NoopLocker

type NoopLocker struct{}

NoopLocker is a lock that does nothing, it always accept lock requests. It should be used when no lock is required.

func (*NoopLocker) Disable

func (l *NoopLocker) Disable(componentName, jobName string) error

Disable does nothing.

func (*NoopLocker) Enable

func (l *NoopLocker) Enable(componentName, jobName string) error

Enable does nothing.

func (*NoopLocker) Lock

func (l *NoopLocker) Lock(componentName, componentID, jobName, jobID string, jobMaxDuration time.Duration) error

Lock does nothing.

func (*NoopLocker) Unlock

func (l *NoopLocker) Unlock(componentName, componentID, jobName, jobID string) error

Unlock does nothing.

type Storage

type Storage interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

Storage is the interface of the database.

Jump to

Keyboard shortcuts

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