dsync

package
v0.1.25 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: LGPL-2.1 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Name  = plugin.Name
	Using = plugin.Using
)
View Source
var (
	// ErrDsync dsync errors.
	ErrDsync = errors.New("dsync")
	// ErrNotAcquired is an error indicating that the distributed lock was not acquired. It is returned by IDistMutex.Unlock and IDistMutex.Extend when the lock was not successfully acquired or has expired.
	ErrNotAcquired = fmt.Errorf("%w: lock is not acquired", ErrDsync)
)

Functions

func GetSeparator

func GetSeparator(servCtx service.Context) string

GetSeparator return name path separator.

func Path

func Path(servCtx service.Context, elems ...string) string

Path return name path.

Types

type DelayFunc

type DelayFunc = generic.Func1[int, time.Duration]

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

type DistMutexOptions added in v0.1.21

type DistMutexOptions struct {
	Expiry        time.Duration
	Tries         int
	DelayFunc     DelayFunc
	DriftFactor   float64
	TimeoutFactor float64
	GenValueFunc  GenValueFunc
	Value         string
}

DistMutexOptions represents the options for acquiring a distributed mutex.

type GenValueFunc

type GenValueFunc = generic.PairFunc0[string, error]

GenValueFunc is used to generate a random value.

type IDistMutex

type IDistMutex interface {
	// Name returns mutex name.
	Name() string
	// Value returns the current random value. The value will be empty until a lock is acquired (or Value option is used).
	Value() string
	// Until returns the time of validity of acquired lock. The value will be zero value until a lock is acquired.
	Until() time.Time
	// Lock locks m. In case it returns an error on failure, you may retry to acquire the lock by calling this method again.
	Lock(ctx context.Context) error
	// Unlock unlocks m and returns the status of unlock.
	Unlock(ctx context.Context) error
	// Extend resets the mutex's expiry and returns the status of expiry extension.
	Extend(ctx context.Context) error
	// Valid returns true if the lock acquired through m is still valid. It may also return true erroneously if quorum is achieved during the call and at
	// least one node then takes long enough to respond for the lock to expire.
	Valid(ctx context.Context) (bool, error)
}

A IDistMutex is a distributed mutual exclusion lock. Avoid sharing the same IDistMutex instance among multiple goroutines. Create a separate IDistMutex instance for each goroutine.

func NewMutex

func NewMutex(servCtx service.Context, name string, settings ...option.Setting[DistMutexOptions]) IDistMutex

NewMutex returns a new distributed mutex with given name.

type IDistSync

type IDistSync interface {
	// NewMutex returns a new distributed mutex with given name.
	NewMutex(name string, settings ...option.Setting[DistMutexOptions]) IDistMutex
	// GetSeparator return name path separator.
	GetSeparator() string
}

IDistSync represents a distributed synchronization mechanism.

type Option

type Option struct{}

Option is a helper struct to provide default options.

func (Option) Default

Default sets the default options for acquiring a distributed mutex.

func (Option) DriftFactor

func (Option) DriftFactor(factor float64) option.Setting[DistMutexOptions]

DriftFactor can be used to set the clock drift factor.

func (Option) Expiry

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

func (Option) GenValueFunc

GenValueFunc can be used to set the custom value generator.

func (Option) RetryDelay

func (Option) RetryDelay(delay time.Duration) option.Setting[DistMutexOptions]

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

func (Option) RetryDelayFunc

func (Option) RetryDelayFunc(fn DelayFunc) option.Setting[DistMutexOptions]

RetryDelayFunc can be used to override default delay behavior.

func (Option) TimeoutFactor

func (Option) TimeoutFactor(factor float64) option.Setting[DistMutexOptions]

TimeoutFactor can be used to set the timeout factor.

func (Option) Tries

func (Option) Tries(tries int) option.Setting[DistMutexOptions]

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

func (Option) Value

Value 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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