lock

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockFailed        = errors.New("unable to get lock")
	ErrProviderNotFound  = errors.New("provider not specified")
	ErrLockAtMostMissing = errors.New("lock time at most not specified")
	ErrTimeout           = errors.New("lock failed: waiting timeout")

	CurrentIp string
)

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	Provider    LockProvider
	Name        string
	LockAtMost  time.Duration
	LockTimeout time.Duration
	LockBy      string
}

type Lock

type Lock interface {
	Unlock() error
}

func LockTask

func LockTask(name string, options ...Options) (Lock, error)

LockTask try to get lock and launch task if succeed. Block if failed to get the lock. If lockTimeout is specified with and positive value, LockTask would wait for at most timeout.

func TryLockTask

func TryLockTask(name string, options ...Options) (Lock, error)

TryLockTask try to get lock and launch task if succeed. It returns error immediately if failed to get the lock. lockTimeout is useless for the method

type LockProvider

type LockProvider interface {
	Lock(Configuration) (Lock, error)

	TryLock(Configuration) (Lock, error)
}

type Options

type Options interface {
	Apply(opt *Configuration)
}

domain interfaces

func WithLockAtMost

func WithLockAtMost(atMost time.Duration) Options

WithLockAtMost specifies the maximum holding time of the lock. If atMost added by now arrives and the task has not finished, the lock would be release by force. If atMost is not positive, the lock will be always held until it's released explicitly.

func WithLockTimeout

func WithLockTimeout(timeout time.Duration) Options

WithLockTimeout specifies the maximum waiting time when required lock is held by others. If timeout added by now arrives and the lock is still unavailable, LockTask would stop waiting and an error would be returned. Timeout is useless for TryLockTask method.

func WithProvider

func WithProvider(provider LockProvider) Options

Directories

Path Synopsis
provides simple distributed lock implemented by Mysql, which depends on Mysql table created in advance.
provides simple distributed lock implemented by Mysql, which depends on Mysql table created in advance.

Jump to

Keyboard shortcuts

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