serverlock

package
v11.1.4-modfix Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LockTimeConfig

type LockTimeConfig struct {
	MaxInterval time.Duration // Duration after which we consider a lock to be dead and overtake it. Make sure this is big enough so that a server cannot acquire the lock while another server is processing.
	MinWait     time.Duration // Minimum time to wait before retrying to acquire the lock.
	MaxWait     time.Duration // Maximum time to wait before retrying to acquire the lock.
}

type RetryOpt

type RetryOpt func(int) error

RetryOpt is a callback function called after each failed lock acquisition try. It gets the number of tries passed as an arg.

type ServerLockExistsError

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

func (*ServerLockExistsError) Error

func (e *ServerLockExistsError) Error() string

type ServerLockService

type ServerLockService struct {
	SQLStore db.DB
	// contains filtered or unexported fields
}

ServerLockService allows servers in HA mode to claim a lock and execute a function if the server was granted the lock It exposes 2 services LockAndExecute and LockExecuteAndRelease, which are intended to be used independently, don't mix them up (ie, use the same actionName for both of them).

func ProvideService

func ProvideService(sqlStore db.DB, tracer tracing.Tracer) *ServerLockService

func (*ServerLockService) LockAndExecute

func (sl *ServerLockService) LockAndExecute(ctx context.Context, actionName string, maxInterval time.Duration, fn func(ctx context.Context)) error

LockAndExecute try to create a lock for this server and only executes the `fn` function when successful. This should not be used at low internal. But services that needs to be run once every ex 10m.

func (*ServerLockService) LockExecuteAndRelease

func (sl *ServerLockService) LockExecuteAndRelease(ctx context.Context, actionName string, maxInterval time.Duration, fn func(ctx context.Context)) error

LockExecuteAndRelease Creates the lock, executes the func, and then release the locks. The locking mechanism is based on the UNIQUE constraint of the actionName in the database (column operation_uid), so a new process can not insert a new operation if already exists one. The parameter 'maxInterval' is a timeout safeguard, if the LastExecution in the database is older than maxInterval, we will assume the lock as timeouted. The 'maxInterval' parameter should be so long that is impossible for 2 processes to run at the same time.

func (*ServerLockService) LockExecuteAndReleaseWithRetries

func (sl *ServerLockService) LockExecuteAndReleaseWithRetries(ctx context.Context, actionName string, timeConfig LockTimeConfig, fn func(ctx context.Context), retryOpts ...RetryOpt) error

LockExecuteAndReleaseWithRetries mimics LockExecuteAndRelease but waits for the lock to be released if it is already taken.

Jump to

Keyboard shortcuts

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