Documentation ¶
Index ¶
- type LockTimeConfig
- type RetryOpt
- type ServerLockExistsError
- type ServerLockService
- func (sl *ServerLockService) LockAndExecute(ctx context.Context, actionName string, maxInterval time.Duration, ...) error
- func (sl *ServerLockService) LockExecuteAndRelease(ctx context.Context, actionName string, maxInterval time.Duration, ...) error
- func (sl *ServerLockService) LockExecuteAndReleaseWithRetries(ctx context.Context, actionName string, timeConfig LockTimeConfig, ...) error
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 ¶
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 ¶
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.