Documentation
¶
Overview ¶
Package lease coordinates keyed locks across processes using a shared backend.
Index ¶
- Variables
- func Digest(parts ...string) string
- func Pause(ctx context.Context, interval time.Duration) error
- type Acquisition
- type Backend
- type ExpiringOptions
- type Holder
- type Lease
- type Lock
- type LockOptions
- type Locker
- type Mode
- type Options
- type Ownership
- type ReadWriteLock
- type RefusedAcquisition
- type Request
- type Result
Constants ¶
This section is empty.
Variables ¶
var ( // ErrBusy is returned by Acquisition.Try when another holder owns the key. ErrBusy = errors.New("lock is busy") // ErrNotOwned is returned by Ownership.Renew when the token is no longer active. ErrNotOwned = errors.New("ownership token is no longer active") )
var ErrLost = errors.New("lease lost")
ErrLost indicates that the library can no longer confirm ownership.
Functions ¶
Types ¶
type Acquisition ¶ added in v0.1.92
type Acquisition interface {
Try(context.Context) (Ownership, error)
Abandon(context.Context) error
}
Acquisition is one attempt sequence for a Request against a store.
type Backend ¶ added in v0.1.92
type Backend struct {
Options Options
TTL time.Duration
Begin func(Request, bool) Acquisition
Holders func(context.Context, Request) ([]Holder, error)
}
Backend is a Locker over a lock store: Begin starts an Acquisition for a Request and Holders (optional) reports who holds a key. Store packages such as lease/redis and lease/kubernetes build one after NormalizeExpiring.
func (*Backend) GroupLock ¶ added in v0.1.92
func (l *Backend) GroupLock(key, group string, options LockOptions) Lock
func (*Backend) Mutex ¶ added in v0.1.92
func (l *Backend) Mutex(key string, options LockOptions) Lock
func (*Backend) ReadWriteLock ¶ added in v0.1.92
func (l *Backend) ReadWriteLock(key string, options LockOptions) ReadWriteLock
type ExpiringOptions ¶
type ExpiringOptions struct {
Options
// TTL defaults to 30 seconds. Specify whole seconds from 3 seconds to 24 hours.
TTL time.Duration
}
ExpiringOptions configures a backend that expires abandoned lease records.
func NormalizeExpiring ¶ added in v0.1.92
func NormalizeExpiring(options ExpiringOptions) (ExpiringOptions, error)
type Holder ¶ added in v0.1.79
type Holder struct {
Name string `json:"name"`
// Mode is "write", "read" or "group".
Mode string `json:"mode"`
Group string `json:"group,omitempty"`
Since time.Time `json:"since"`
}
Holder is one current holder of a key, as reported by Lock.Holders.
type Lock ¶
type Lock interface {
Lock(context.Context) (Lease, error)
TryLock(context.Context) (Lease, bool, error)
LockWait(context.Context) <-chan Result
// Holders lists the current holders of the key. It is diagnostic: the
// answer can be stale by the time it returns.
Holders(context.Context) ([]Holder, error)
}
Lock acquires a mutex or a share of a group.
type LockOptions ¶ added in v0.1.79
type LockOptions struct {
// Holder names the caller in Holders reports. It is required: printable
// UTF-8 of at most 128 bytes.
Holder string
}
LockOptions describe the caller of an acquisition.
type Locker ¶
type Locker interface {
Mutex(key string, options LockOptions) Lock
ReadWriteLock(key string, options LockOptions) ReadWriteLock
GroupLock(key, group string, options LockOptions) Lock
}
Locker creates handles in a backend's configured scope. Keys and groups match ^[A-Za-z0-9_-]+$ and are at most 100 characters; acquisition fails otherwise.
func NewFile ¶ added in v0.1.79
NewFile coordinates the processes of one host through flock files under dir: <dir>/<digest>.lock holds the lock and <dir>/<digest>.intent queues a waiting writer ahead of new readers. Each holder also keeps a record in <dir>/<digest>.holders/, flocked for the life of its lease, which Holders reads back. A lease lasts until it is unlocked or its process exits and is never lost. GroupLock is refused.
type Options ¶
type Options struct {
// Scope matches ^[A-Za-z0-9_-]+$ and is at most 100 characters.
Scope string
RetryInterval time.Duration
OperationTimeout time.Duration
}
Options configures the coordination scope and bounds individual backend calls.
type ReadWriteLock ¶
type ReadWriteLock interface {
Lock
RLock(context.Context) (Lease, error)
TryRLock(context.Context) (Lease, bool, error)
RLockWait(context.Context) <-chan Result
}
ReadWriteLock admits concurrent readers or an exclusive writer.
type RefusedAcquisition ¶ added in v0.1.92
type RefusedAcquisition struct{ Err error }
RefusedAcquisition fails every attempt with err, for a request a backend cannot name.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package leasetest holds the ginkgo contract every lease.Locker backend must pass.
|
Package leasetest holds the ginkgo contract every lease.Locker backend must pass. |
|
internal/redsync
Package redsync provides a Redis-based distributed mutual exclusion lock implementation as described in the post https://redis.io/docs/latest/develop/clients/patterns/distributed-locks/.
|
Package redsync provides a Redis-based distributed mutual exclusion lock implementation as described in the post https://redis.io/docs/latest/develop/clients/patterns/distributed-locks/. |