Documentation ¶
Index ¶
Constants ¶
const ( // MinLockExpire is the minimum lock expire time. MinLockExpire = 1000 // DefaultLockExpire is the default lock expire time. DefaultLockExpire = 3000 // MaxRefreshRetryTimes is the maximum retry times of // auto refresh if the refresh fails continuously. MaxRefreshRetryTimes = 100 )
Variables ¶
var ( // ErrLockWithoutName is returned when creating a lock with a empty name. ErrLockWithoutName = errors.New("empty lock name") // ErrLockExpireTooSmall is returned when creating a lock with expire smaller than 300ms. ErrLockExpireTooSmall = errors.New("lock expiration too small") // ErrAlreadyAcquired is returned when try to lock an already acquired lock. ErrAlreadyAcquired = errors.New("lock already acquired") // ErrNotAcquired is returned when a lock cannot be acquired. ErrNotAcquired = errors.New("lock not acquired") // ErrLockNotHeld is returned when trying to release an unacquired lock. ErrLockNotHeld = errors.New("lock not held") )
Functions ¶
This section is empty.
Types ¶
type RedLock ¶
type RedLock struct {
// contains filtered or unexported fields
}
RedLock represents a redis lock.
func New ¶
New creates and returns a new redis lock.
Note that a distributed lock without expire time is dangerous, so expire time is always required. If no expire given, say expire <= 0, a default 3 seconds expire time will be used.
Furthermore a very small expire time, such as 5ms, does not make sense at all. The lock possibely has already expired after the caller just acquired it considering the network communication time between the caller and redis server. Always give an expiration larger than 1s.
A lock with autoRefresh enabled will refresh its expiration periodically in a background goroutine so that the lock holder can hold the lock all the time before releasing it. The refresh interval is always the 2/3 of lock expiration.