Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnlock = errors.New("unlock failed, name or unique id incorrect")
ErrUnlock is the error thrown when you try to unlock a lock that's expired or a lock that was locked by another unique id.
Functions ¶
This section is empty.
Types ¶
type Locker ¶
type Locker interface {
// Lock attempts to acquire a lock identified by name for the given duration.
// uniqueID needs to be some sort of unique identifier that is different among
// all hosts attempting to acquire the lock.
Lock(name, uniqueID string, duration time.Duration) (bool, error)
// Unlock attempts to unlock a lock, but only if the uniqueID is the one that
// has the lock.
Unlock(name, uniqueID string) error
}
Locker defines the interface for the backend lock system.
type RedisLocker ¶
type RedisLocker struct {
// contains filtered or unexported fields
}
RedisLocker is a Redis backed Locker implementation.
func NewRedisLocker ¶
func NewRedisLocker(pool *redis.Pool, prefix string) *RedisLocker
NewRedisLocker initializes a new RedisLocker.
func (*RedisLocker) Lock ¶
Lock attempts to acquire the lock. Returns true if the lock was acquired.
func (*RedisLocker) Unlock ¶
func (l *RedisLocker) Unlock(name, uniqueID string) error
Unlock attempts to unlock a lock, but only if the uniqueID is the one that has the lock.
type UniqueLocker ¶
type UniqueLocker struct {
// contains filtered or unexported fields
}
UniqueLocker wraps a Locker and uniqueID and calls the Locker's lock/unlock functions with the uniqueID to simplify passing around the uniqueID (pass the UniqueLocker instead).
func NewUniqueLocker ¶
func NewUniqueLocker(l Locker, uniqueID string) *UniqueLocker
NewUniqueLocker initializes a UniqueLocker.
func (*UniqueLocker) Lock ¶
Lock attempts to acquire the lock identified by name. Returns true if the lock was acquired.
func (*UniqueLocker) Refresh ¶
func (l *UniqueLocker) Refresh(ctx context.Context, boff backoff.Backoffer, lockName string, lockDuration, refreshPeriod time.Duration)
Refresh will periodically refresh the lock (attempts to refresh at refreshPeriod) until the context is cancelled. This assumes the UniqueLocker has already acquired the lock. This method should be run in a goroutine.
func (*UniqueLocker) Unlock ¶
func (l *UniqueLocker) Unlock(name string) error
Unlock attempts to unlock the lock identified by name.