rwlock

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnection is returned by Locker methods in case of problems with redis.
	ErrConnection = errors.New("redis connection error")
	// ErrTimeout is returned by Locker methods if timeout was specified and was exceeded while waiting for lock.
	ErrTimeout = errors.New("timeout exceeded but lock not acquired")
	// ErrInterrupted is returned by Locker methods if they were interrupted via Context.
	ErrInterrupted = errors.New("interrupted")
	// ErrNotReleased is returned by locker methods if lock was not released.
	ErrNotReleased = errors.New("lock was not released")
	// ErrUnknownMode is return by locker methods in case the lock was set to unknown mode.
	ErrUnknownMode = errors.New("lock is in unknown mode")
)

Functions

This section is empty.

Types

type Locker

type Locker interface {
	// Read executes given function with shared reader access.
	Read(fn func()) error
	// Write executes given function with unique writer access.
	Write(fn func()) error
}

Locker allows to execute given functions at reader or writer access privilege.

func New

func New(redisClient *redis.Client, keyLock, keyReadersCount, keyWriterIntent string, opts Options) Locker

New instance of RW-Locker. keyLock, keyReadersCount, keyWriterIntent must be unique keys that will be used by locker implementation.

type Mode

type Mode int

Mode of the lock behavior.

const (
	// ModeUndefined will trigger default option to be used.
	ModeUndefined Mode = iota
	// ModePreferReader makes the writer and reader to have equal priority.
	ModePreferReader
	// ModePreferWriter makes the writer to have higher priority over the reader.
	ModePreferWriter
)

type Options

type Options struct {
	// LockTTL sets lock duration timeout.
	// This allows to release the lock even if whole program crashes.
	// Recommended not to make it too big or too small as it may affect performance.
	// It should be less than RetryCount * RetryInterval in order to avoid undesired ErrTimeouts.
	// Minimum: 100 milliseconds
	// Default: 1 second
	LockTTL time.Duration

	// RetryCount limits number of attempts to acquire lock.
	// Default: 200
	RetryCount int

	// RetryInterval sets interval between attemts to acquire lock.
	// Minimum: 1 millisecond
	// Default: 10 milliseconds
	RetryInterval time.Duration

	// Context of the execution.
	// Default: Background
	Context context.Context

	// AppID is used as writer lock token prefix.
	// Used for debugging.
	AppID string

	// ReaderLockToken should be the same for all readers group.
	// You can override default token here to create subgroups of readers.
	ReaderLockToken string

	// Mode of the lock behavior.
	// Defaults to writer-preferring behavior in order not to break back compatibility.
	// Default: ModePreferWriter
	Mode Mode
}

Options used to configure locker.

Jump to

Keyboard shortcuts

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