Documentation
¶
Overview ¶
Package rlock provides a distributed locking mechanism using Redis.
Index ¶
- Constants
- type Lock
- type Locker
- func (l *Locker) Acquire(key string, ttl time.Duration, timeout time.Duration) (*Lock, error)
- func (l *Locker) SetInitialInterval(initialInterval time.Duration) *Locker
- func (l *Locker) SetMaxInterval(maxInterval time.Duration) *Locker
- func (l *Locker) SetNameSpace(nameSpace string) *Locker
- func (l *Locker) TryAcquire(key string, ttl time.Duration) (*Lock, error)
Constants ¶
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) Release ¶
Release releases the lock. If the lock is already released, it returns an error.
func (*Lock) TryRefresh ¶
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 (*Locker) Acquire ¶
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 ¶
SetInitialInterval sets the initial interval between attempts to acquire a lock.
func (*Locker) SetMaxInterval ¶
SetMaxInterval sets the maximum interval between attempts to acquire a lock.
func (*Locker) SetNameSpace ¶
SetNameSpace sets the namespace for locks redis keys that will be use as a prefix spearagted from key by double colon.