lock

package
v1.0.4-gitspaces-beta Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Functions

func SplitKey

func SplitKey(uniqKey string) (namespace, key string)

Types

type Config

type Config struct {
	App       string // app namespace prefix
	Namespace string
	Provider  Provider
	Expiry    time.Duration

	Tries      int
	RetryDelay time.Duration
	DelayFunc  DelayFunc

	DriftFactor   float64
	TimeoutFactor float64

	GenValueFunc func() (string, error)
	Value        string
}

type DelayFunc

type DelayFunc func(tries int) time.Duration

A DelayFunc is used to decide the amount of time to wait between retries.

type Error

type Error struct {
	Kind ErrorKind
	Key  string
	Err  error
}

Error is custom unique type for all type of errors.

func NewError

func NewError(kind ErrorKind, key string, err error) *Error

func (Error) Error

func (e Error) Error() string

Error implements error interface.

type ErrorKind

type ErrorKind string

ErrorKind enum displays human readable message in error.

const (
	ErrorKindLockHeld            ErrorKind = "lock already held"
	ErrorKindLockNotHeld         ErrorKind = "lock not held"
	ErrorKindProviderError       ErrorKind = "lock provider error"
	ErrorKindCannotLock          ErrorKind = "timeout while trying to acquire lock"
	ErrorKindContext             ErrorKind = "context error while trying to acquire lock"
	ErrorKindMaxRetriesExceeded  ErrorKind = "max retries exceeded to acquire lock"
	ErrorKindGenerateTokenFailed ErrorKind = "token generation failed"
)

type InMemory

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

InMemory is a local implementation of a MutexManager that it's intended to be used during development.

func NewInMemory

func NewInMemory(config Config) *InMemory

NewInMemory creates a new InMemory instance only used for development.

func (*InMemory) NewMutex

func (m *InMemory) NewMutex(key string, options ...Option) (Mutex, error)

NewMutex creates a mutex for the given key. The returned mutex is not held and must be acquired with a call to .Lock.

type Mutex

type Mutex interface {
	// Key returns the key to be locked.
	Key() string

	// Lock acquires the lock. It fails with error if the lock is already held.
	Lock(ctx context.Context) error

	// Unlock releases the lock. It fails with error if the lock is not currently held.
	Unlock(ctx context.Context) error
}

type MutexManager

type MutexManager interface {
	// NewMutex creates a mutex for the given key. The returned mutex is not held
	// and must be acquired with a call to .Lock.
	NewMutex(key string, options ...Option) (Mutex, error)
}

MutexManager describes a Distributed Lock Manager.

func ProvideMutexManager

func ProvideMutexManager(config Config, client redis.UniversalClient) MutexManager

type Option

type Option interface {
	Apply(*Config)
}

An Option configures a mutex.

func WithDriftFactor

func WithDriftFactor(factor float64) Option

WithDriftFactor can be used to set the clock drift factor.

func WithExpiry

func WithExpiry(expiry time.Duration) Option

WithExpiry can be used to set the expiry of a mutex to the given value.

func WithGenValueFunc

func WithGenValueFunc(genValueFunc func() (string, error)) Option

WithGenValueFunc can be used to set the custom value generator.

func WithNamespace

func WithNamespace(ns string) Option

WithNamespace returns an option that configures Mutex.ns.

func WithRetryDelay

func WithRetryDelay(delay time.Duration) Option

WithRetryDelay can be used to set the amount of time to wait between retries.

func WithRetryDelayFunc

func WithRetryDelayFunc(delayFunc DelayFunc) Option

WithRetryDelayFunc can be used to override default delay behavior.

func WithTimeoutFactor

func WithTimeoutFactor(factor float64) Option

WithTimeoutFactor can be used to set the timeout factor.

func WithTries

func WithTries(tries int) Option

WithTries can be used to set the number of times lock acquire is attempted.

func WithValue

func WithValue(v string) Option

WithValue can be used to assign the random value without having to call lock. This allows the ownership of a lock to be "transferred" and allows the lock to be unlocked from elsewhere.

type OptionFunc

type OptionFunc func(*Config)

OptionFunc is a function that configures a mutex.

func (OptionFunc) Apply

func (f OptionFunc) Apply(config *Config)

Apply calls f(config).

type Provider

type Provider string
const (
	MemoryProvider Provider = "inmemory"
	RedisProvider  Provider = "redis"
)

type Redis

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

Redis wrapper for redsync.

func NewRedis

func NewRedis(config Config, client redislib.UniversalClient) *Redis

NewRedis create an instance of redisync to be used to obtain a mutual exclusion lock.

func (*Redis) NewMutex

func (r *Redis) NewMutex(key string, options ...Option) (Mutex, error)

Acquire new lock.

type RedisMutex

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

func (*RedisMutex) Key

func (l *RedisMutex) Key() string

Key returns the key to be locked.

func (*RedisMutex) Lock

func (l *RedisMutex) Lock(ctx context.Context) error

Lock acquires the lock. It fails with error if the lock is already held.

func (*RedisMutex) Unlock

func (l *RedisMutex) Unlock(ctx context.Context) error

Unlock releases the lock. It fails with error if the lock is not currently held.

Jump to

Keyboard shortcuts

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