lock

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2024 License: MIT Imports: 14 Imported by: 0

README

Go Reference Coverage Status GitHub go.mod Go version GitHub release (latest by date) Go Report Card Actions Status

utte-lock

Resource locks for the UTTE project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DbMigrations

func DbMigrations(conf config.Config, secretsManager secrets.SecretsManager) (*migrate.Migrate, error)

DbMigrations creates a db migrate instance. Location of migration scripts have to be passed via config. Example:

db:
  migrations: "./db/migrations/"

func MigrationSucceeded

func MigrationSucceeded(err error) bool

MigrationSucceeded verifies than an error return from migration up is nil or at least no changes has happend.

Types

type Backoff

type Backoff interface {

	// Reset number of attempts to start from scratch.
	Start()

	// Next calculates durection a client should eait for next attempts to get a lock.
	Next() *time.Duration

	// Attempts returns the number of alreadz executed circles.
	Attempts() int

	// MaxAttempts returns the maximun number of attempts used by a backoff.
	MaxAttempts() int
}

Backoff is used for retry if a lock can not be obtained directly.

func NewBackoff

func NewBackoff() Backoff

NewBackoff returns a backoff with default values. MaxAttempts: 3. InitialInterval: 1s, Nultiplier: 1.5

func NewExponentialBackoff

func NewExponentialBackoff(attempts int, initialInterval time.Duration, multiplier float64) Backoff

NewExponentialBackoff returns new backoff with given values.

type DatabaseLock

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

DatabaseLock is an instance of Lock using a database to manage lock entries.

func (*DatabaseLock) Obtain

func (lock *DatabaseLock) Obtain(resourceId model.Identifier, clientId model.Identifier) (*Lease, error)

Obtain will try to get a lock for given resource using a default backoff. See NewBackoff().

func (*DatabaseLock) ObtainWithBackoff

func (lock *DatabaseLock) ObtainWithBackoff(resourceId model.Identifier, clientId model.Identifier, backoff Backoff) (*Lease, error)

ObtainWithBackoff will tru to get a lock and uses given backoff if first ot follow up attempts fail.

func (*DatabaseLock) ObtainWithNoWait

func (lock *DatabaseLock) ObtainWithNoWait(resourceId model.Identifier, clientId model.Identifier) (*Lease, error)

ObtainWithNoWait will only run one attempt to get a lock.

func (*DatabaseLock) Release

func (lock *DatabaseLock) Release(lease *Lease) error

Release deletes given lock and should be used after all operations for a resource has been performed. Sure, a lock will expire, but you should ensure efficient usage of resource locks.

type ExponentialBackoff

type ExponentialBackoff struct {

	// InitialInterval is the initial wait duration for a second attempt.
	InitialInterval time.Duration

	// Multiplier is applied to wait interval at each attempt.
	Multiplier float64
	// contains filtered or unexported fields
}

ExponentialBackoff is used to retry obtaining a lock.

func (*ExponentialBackoff) Attempts

func (backoff *ExponentialBackoff) Attempts() int

Attempts returns number of current attempts.

func (*ExponentialBackoff) MaxAttempts

func (backoff *ExponentialBackoff) MaxAttempts() int

MaxAttempts returns number of max attempts a backoff is confibured for.

func (*ExponentialBackoff) Next

func (backoff *ExponentialBackoff) Next() *time.Duration

Next calculates duration until next attempt should be run.

func (*ExponentialBackoff) Start

func (backoff *ExponentialBackoff) Start()

Start reset current attempts to 0.

type Lease

type Lease struct {

	// ResourceId is an identifier of a locked resource.
	ResourceId model.Identifier

	// ClientId is a source application id which obtains a lock for a resource.
	ClientId model.Identifier

	// Expiry, UTC timestamp when a lock gets invalid.
	Expiry time.Time

	// Sequence, serials number of a lockused to avoid resource changes by deprecated locks.
	Sequence int
}

Lease is a temprary lock of a resource.

type Lock

type Lock interface {

	// Obtain will try to get a lock for given resource by using a default backoff. s. NewBackoff() for details.
	Obtain(resourceId model.Identifier, clientId model.Identifier) (*Lease, error)

	// ObtainWithBackoff will try to lock passed resource and uses given backoff for retry.
	ObtainWithBackoff(resourceId model.Identifier, clientId model.Identifier, backoff Backoff) (*Lease, error)

	// ObtainWithNoWait will fail if a lock for given resource can not be created immedeately.
	ObtainWithNoWait(resourceId model.Identifier, clientId model.Identifier) (*Lease, error)

	// Release should be used to clean a lock if it's no longer required.
	// Locks expired after a defined time, so it's not mandatory to call this method - but this will keep a lock for a unnecessary long time and may block other clients.
	Release(lease *Lease) error
}

Lock is unused to obtain and release resource locks.

Jump to

Keyboard shortcuts

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