rwlock

package module
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: 2 Imported by: 0

README

redis-rwlock

Golang implementation of distributed RW-Lock using Redis.

This implementation was inspired by Redis distributed locking mechanism and uses Redis LUA scripts. Configuration:

Option Default Minumum Description
LockTTL 1 sec 100 msec
RetryCount 200 0 Lock acquisition internally is a non-blocking operation. To imitate normal blocking behavior we make RetryCount attempts to acquire the lock with RetryInterval intervals. If lock is not acquired after all attempts caller receives timeout error.
RetryInterval 10 msec 1 msec Interval between lock acquisition attempts. See RetryCount description.
Context context.Background n/a Execution context. All locking operation use this context. You can use cancelable context for graceful shutdown.
AppID "" n/a Any string which will allow you to debug redis calls and locks to detect interference of multiple apps using the same lock.
ReaderLockToken "read_c2d-75a1-4b5b-a6fb-b0754224c666" n/a This token is used as a value for the global lock key when reader acquires the lock. It should be the same for all readers because when tle lock is released we check if it was acquired by us using this token and raise error if not. But if you have separate groups of readers which should not interfere you can use different token to distinguish them.
Mode ModePreferWriter n/a RWMutex can work in two modes: either reader and writer are equal or readers respect writer and do not acquire lock if writers has declared it's intention to acquire the lock. By default ModePreferWriter is set to keep back compatibility. See [MutexMode](#Mutex Modes)
Mutex Modes

Generally RWMutex is a combination of regular mutex and a counter of readers. Lock is either free or acquired exclusively by the writer or by a number of readers. First reader acquires lock and increments readers counter. Every new reader increments number of readers and does not do anything with the global lock. When reader releases the lock it first decrements readers counter and if reader was the last one it releases the global lock. That's it.

Reader-preferring RWMutex is simplest implementation. If lock is acquired by reader(s) writer will block but new reader will increment counter and keep global lock acquired. In this case readers may never release lock for the writer and continuously read outdated data.

Writer-preferring RWMutex introduce 'Writer Intention'. In this case if lock is acquired by reader(s) and writer tries to acquire the lock it will set an intention. Writer-respecting readers will not increment readers counter and writer will acquire the global lock as soon as all old readers will finish their job.

Implementation

Every lock operation (rlock, runlock, lock, unlock, refresh) is a single LUA script. You can find scripts with comments in lua directory.

Documentation

Overview

Package rwlock is an adapter package to pkg/rwlock. Consider using pkg/rwlock package in new projects as this file may be eventually removed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

type Locker = rwlock.Locker

Locker is an alias type to #rwlock.Locker

func Make

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

Make new instance of RW-Locker. Deprecated due to incorrect naming of the function. Use #rwlock.New instead.

func New added in v1.1.0

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

New instance of RW-Locker. Use #rwlock.New instead.

type Options

type Options = rwlock.Options

Options is an alias type to #rwlock.Options

Directories

Path Synopsis
examples
pkg

Jump to

Keyboard shortcuts

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