rlock

package module
v0.1.0 Latest Latest
Warning

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

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

README

rlock

rlock is a Redis-based distributed locking library for Go. It provides a simple API for acquiring and releasing locks, and uses Redis to ensure that locks are properly distributed across multiple processes.

Installation

To install rlock, use go get:

go get github.com/ksysoev/rlock

Usage

Here's an example of how to use rlock to acquire and release a lock:

redisClient := redis.NewClient(getRedisOptions())
l := rlock.NewLocker(context.Background(), redisClient)

lock, err := l.TryAcquire("my-lock", 1*time.Second)
if err != nil {
    fmt.Println("Unable to acquire lock:", err)
}

// It'll wait untill lock will be relesed or timeout is reach. 
lock1, err := l.Acquire("my-lock", 1*time.Second, 2*time.Second)
if err != nil {
    fmt.Println("Unable to acquire lock:", err)
}
defer lock1.Release()

Contributing

Contributions to rlock are welcome!

License

rlock is licensed under the MIT License

Documentation

Overview

Package rlock provides a distributed locking mechanism using Redis.

Index

Constants

View Source
const (
	// DefaultNameSpace is the default namespace for locks.
	DefaultNameSpace = "rlock"
	// DefaultMaxInterval is the default maximum interval between attempts to acquire a lock.
	DefaultMaxInterval = 5 * time.Second
	// DefaultInitialInterval is the default initial interval between attempts to acquire a lock.
	DefaultInitialInterval = 500 * time.Millisecond
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Lock

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

Lock is a struct that represents a distributed lock.

func (*Lock) IsLocked

func (l *Lock) IsLocked() bool

IsLocked returns true if the lock is still held, false otherwise.

func (*Lock) Release

func (l *Lock) Release() error

Release releases the lock. If the lock is already released, it returns an error.

func (*Lock) TryRefresh

func (l *Lock) TryRefresh() error

TryRefresh tries to refresh the lock. if lock in redis still belongs to the current instance, it will update ttl. If the lock is overtaken, it returns an error.

type Locker

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

Locker is a struct that represents a distributed lock manager.

func NewLocker

func NewLocker(ctx context.Context, storage *redis.Client) *Locker

NewLocker creates a new Locker instance.

func (*Locker) Acquire

func (l *Locker) Acquire(key string, ttl time.Duration, timeout time.Duration) (*Lock, error)

Acquire tries to acquire a lock for the given key. It uses SetNX Redis command to set the key-value pair only if the key does not exist. If the lock is acquired successfully, it returns a new Lock instance with the given key, Locker instance, unique ID and time-to-live (TTL). If the lock is not acquired, it retries after a certain interval using exponential backoff algorithm until the timeout is reached. If the timeout is reached, it returns an error. Parameters: - key: string representing the key to acquire the lock for. - ttl: time.Duration representing the time-to-live of the lock. - timeout: time.Duration representing the maximum time to wait for the lock to be acquired. Returns: - *Lock: a new Lock instance if the lock is acquired successfully. - error: an error if the lock is not acquired.

func (*Locker) SetInitialInterval

func (l *Locker) SetInitialInterval(initialInterval time.Duration) *Locker

SetInitialInterval sets the initial interval between attempts to acquire a lock.

func (*Locker) SetMaxInterval

func (l *Locker) SetMaxInterval(maxInterval time.Duration) *Locker

SetMaxInterval sets the maximum interval between attempts to acquire a lock.

func (*Locker) SetNameSpace

func (l *Locker) SetNameSpace(nameSpace string) *Locker

SetNameSpace sets the namespace for locks redis keys that will be use as a prefix spearagted from key by double colon.

func (*Locker) TryAcquire

func (l *Locker) TryAcquire(key string, ttl time.Duration) (*Lock, error)

TryAcquire tries to acquire a lock for the given key with the specified time-to-live (TTL).

Jump to

Keyboard shortcuts

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