Documentation ¶
Overview ¶
package zookeeper implements a ZooKeeper based Lock.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAlreadyOwnLock is returned if Lock is called with a context holding an // OwnerKey equal to that of an active lock. ErrAlreadyOwnLock = errors.New("requestor already has an active lock") // ErrLockingTimedOut is returned when a lock couldn't be acquired by the // context deadline. ErrLockingTimedOut = errors.New("attempt to acquire lock timed out") // ErrInvalidSeqNode is returned when sequential znodes are being parsed for // a trailing integer ID, but one isn't found. ErrInvalidSeqNode = errors.New("znode doesn't appear to be a sequential type") // ErrNotLockOwner is returned when Unlock is attempting to be called where the // requestor's OwnerKey value does not equal the current lock owner. ErrNotLockOwner = errors.New("non-owner attempted to call unlock") // ErrOwnerAlreadySet is returned when SetOwner is being called on a lock where // the owner field is non-nil. ErrOwnerAlreadySet = errors.New("attempt to set owner on a claimed lock") )
Functions ¶
This section is empty.
Types ¶
type ErrExpireLockFailed ¶ added in v3.15.0
type ErrExpireLockFailed struct {
// contains filtered or unexported fields
}
ErrExpireLockFailed is returned when a lock with an expired TTL fails to purge.
func (ErrExpireLockFailed) Error ¶ added in v3.15.0
func (err ErrExpireLockFailed) Error() string
Error returns an error string.
type ErrLockingFailed ¶
type ErrLockingFailed struct {
// contains filtered or unexported fields
}
ErrLockingFailed is a general failure.
func (ErrLockingFailed) Error ¶
func (err ErrLockingFailed) Error() string
Error returns an error string.
type ErrUnlockingFailed ¶
type ErrUnlockingFailed struct {
// contains filtered or unexported fields
}
ErrUnlockingFailed is a general failure.
func (ErrUnlockingFailed) Error ¶
func (err ErrUnlockingFailed) Error() string
Error returns an error string.
type LockEntries ¶
type LockEntries struct {
// contains filtered or unexported fields
}
LockEntries is a container of locks.
func (LockEntries) First ¶
func (le LockEntries) First() (int, error)
First returns the ID with the lowest value.
type ZooKeeperClient ¶
type ZooKeeperClient interface { Children(string) ([]string, *zk.Stat, error) Create(string, []byte, int32, []zk.ACL) (string, error) CreateProtectedEphemeralSequential(string, []byte, []zk.ACL) (string, error) Delete(string, int32) error Get(string) ([]byte, *zk.Stat, error) GetW(string) ([]byte, *zk.Stat, <-chan zk.Event, error) }
ZooKeeperClient interface.
type ZooKeeperLock ¶
type ZooKeeperLock struct { Path string OwnerKey string TTL int // contains filtered or unexported fields }
ZooKeeperLock implements a Lock.
func NewZooKeeperLock ¶
func NewZooKeeperLock(c ZooKeeperLockConfig) (*ZooKeeperLock, error)
NewZooKeeperLock returns a ZooKeeperLock.
func NewZooKeeperLockWithClient ¶
func NewZooKeeperLockWithClient(cfg ZooKeeperLockConfig, zkc ZooKeeperClient) (*ZooKeeperLock, error)
NewZooKeeperLock takes a ZooKeeperLockConfig and ZooKeeperClient and returns a ZooKeeperLock. Any initialization (such as path creation) should be performed outside of this initializer.
func (*ZooKeeperLock) Lock ¶
func (z *ZooKeeperLock) Lock(ctx context.Context) error
Lock attemps to acquire a lock. If the lock cannot be acquired by the context deadline, the lock attempt times out.
func (*ZooKeeperLock) Owner ¶ added in v3.13.1
func (z *ZooKeeperLock) Owner() interface{}
Owner returns the current lock owner.
func (*ZooKeeperLock) Unlock ¶
func (z *ZooKeeperLock) Unlock(ctx context.Context) error
Unlock releases a lock.
func (*ZooKeeperLock) UnlockLogError ¶ added in v3.15.0
func (z *ZooKeeperLock) UnlockLogError(ctx context.Context)
Unlock releases a lock and logs, rather than returning, any errors if encountered.
type ZooKeeperLockConfig ¶
type ZooKeeperLockConfig struct { // The address of the ZooKeeper cluster. Address string // The locking path; this is the register that locks are attempting to acquire. Path string // A non-zero TTL sets a limit (in milliseconds) on how long a lock is possibly // valid for. Once this limit is exceeded, any new lock claims can destroy those // exceeding their TTL. TTL int // An optional lock ownership identifier. Context values can be inspected to // determine if a lock owner already has the lock. For example, if we specify // an OwnerKey configuration value of UserID, any successful lock claim will // set the lock owner as the value of UserID from the context received. Any // successive calls to Lock() with the same UserID context value will also // succeed. As a safety, this is not a distributed feature and is scoped to the // ZooKeeperLock instance; attempting to have two processes claim a lock // on the same path with the same OwnerKey/value will result in only one lock // being granted. This also prevents a concurrent program sharing a ZooKeeperLock // from allowing requestors to call Unlock on a lock that it doesn't own. OwnerKey string }
ZooKeeperLockConfig holds ZooKeeperLock configurations.