redis_mutex

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 6 Imported by: 0

README

Go Redis Mutex

build workflow PkgGoDev GitHub last commit

go-redis-mutex is a Go package that offers an implementation of a distributed mutex based on Redis. This package provides both a standard mutex and a read/write mutex.

Locking implementation recommendations were taken from KeyDB documentation.

Dependencies

Installation

go get github.com/i4erkasov/go-redis-mutex

Usage

import "github.com/i4erkasov/go-redis-mutex"

// Create a new mutex
mutex := redis_mutex.NewMutex(client, "myMutexKey")

// Attempt to lock
err := mutex.Lock(context.Background())

// Attempt to unlock
err = mutex.Unlock(context.Background())

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMutexOwnershipConflict is an error returned when trying to unlock
	// a mutex that has been released or is held by another process.
	ErrMutexOwnershipConflict = errors.New("mutex already released or acquired by someone else")
)

Functions

This section is empty.

Types

type KeyDBMutex

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

KeyDBMutex provides a mutex mechanism using Redis.

func NewMutex

func NewMutex(client *redis.Client, key string) *KeyDBMutex

NewMutex initializes a new Redis-based mutex. The mutex uses a random value to ensure that only the owner can unlock it.

func (*KeyDBMutex) Lock

func (m *KeyDBMutex) Lock(ctx context.Context) error

Lock tries to obtain the lock. If it's already held, it will keep trying at intervals until the lock is acquired.

func (*KeyDBMutex) TryLock

func (m *KeyDBMutex) TryLock(ctx context.Context) (bool, error)

TryLock attempts to acquire the lock once. If successful, it returns true.

func (*KeyDBMutex) Unlock

func (m *KeyDBMutex) Unlock(ctx context.Context) error

Unlock releases the lock. If the lock is held by another value (i.e., acquired by another process or thread), it returns an error.

type KeyDBRWMutex

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

func NewRWMutex

func NewRWMutex(client *redis.Client, baseKey string, writeVal string) *KeyDBRWMutex

NewRWMutex initializes a new Redis-based reader-writer mutex. The mutex separates the keys for read and write locks for granular control. It uses a specific value for write locks to ensure that only the owner can unlock it, while read locks increment a counter to manage multiple readers.

func (*KeyDBRWMutex) Lock

func (m *KeyDBRWMutex) Lock(ctx context.Context) error

Lock attempts to acquire a write lock, waiting if there are readers or another writer.

func (*KeyDBRWMutex) RLock

func (m *KeyDBRWMutex) RLock(ctx context.Context) error

RLock attempts to acquire a read lock, waiting if a write lock is held.

func (*KeyDBRWMutex) RUnlock

func (m *KeyDBRWMutex) RUnlock(ctx context.Context)

RUnlock releases a read lock by decrementing the read count.

func (*KeyDBRWMutex) Unlock

func (m *KeyDBRWMutex) Unlock(ctx context.Context) error

Unlock releases a write lock, but only if the caller owns the lock.

type Mutex

type Mutex interface {
	Lock(ctx context.Context) error
	Unlock(ctx context.Context) error
	TryLock(ctx context.Context) (bool, error)
}

type RWMutex

type RWMutex interface {
	RLock(ctx context.Context) error
	RUnlock(ctx context.Context)
	Lock(ctx context.Context) error
	Unlock(ctx context.Context) error
}

Jump to

Keyboard shortcuts

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