Documentation ¶
Overview ¶
Package lock provides distributed locking backed by MongoDB.
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) CreateIndexes(ctx context.Context) error
- func (c *Client) Renew(ctx context.Context, lockId string, ttl uint) ([]LockStatus, error)
- func (c *Client) SLock(ctx context.Context, resourceName, lockId string, ld LockDetails, ...) error
- func (c *Client) Status(ctx context.Context, f Filter) ([]LockStatus, error)
- func (c *Client) Unlock(ctx context.Context, lockId string) ([]LockStatus, error)
- func (c *Client) XLock(ctx context.Context, resourceName, lockId string, ld LockDetails) error
- type Filter
- type LockDetails
- type LockStatus
- type LockStatusesByCreatedAtDesc
- type Purger
Constants ¶
const ( // LOCK_TYPE_EXCLUSIVE is the string representation of an exclusive lock. LOCK_TYPE_EXCLUSIVE = "exclusive" // LOCK_TYPE_SHARED is the string representation of a shared lock. LOCK_TYPE_SHARED = "shared" )
Variables ¶
var ( // ErrAlreadyLocked is returned by a locking operation when a resource // is already locked. ErrAlreadyLocked = errors.New("unable to acquire lock (resource is already locked)") // ErrLockNotFound is returned when a lock cannot be found. ErrLockNotFound = errors.New("unable to find lock") UPSERT = true ReturnDoc = options.After )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
A Client is a distributed locking client backed by MongoDB. It requires a resource name (the object that gets locked) and a lockId (lock identifier) when creating a lock. Multiple locks can be created with the same lockId, making it easy to unlock or renew a group of related locks at the same time. Another reason for using lockIds is to ensure that only the process that creates a lock knows the lockId needed to unlock it - knowing a resource name alone is not enough to unlock it.
func NewClient ¶
func NewClient(collection *mongo.Collection) *Client
NewClient creates a new Client.
func (*Client) CreateIndexes ¶
CreateIndexes creates the required and recommended indexes for mongo-lock in the client's database. Indexes that already exist are skipped.
func (*Client) Renew ¶
Renew updates the TTL of all locks with the associated lockId. The new TTL for the locks will be the value of the argument provided. Only locks with a TTL greater than or equal to 1 can be renewed, so Renew will return an error if any of the locks with the associated lockId have a TTL of 0. This is to prevent a potential race condition beteween renewing locks and purging expired ones.
A LockStatus struct is returned for each lock that is renewed. The TTL field in each struct represents the new TTL. This information can be used to ensure that all of the locks created with the lockId have been renewed. If not all locks have been renewed, or if an error is returned, the caller should assume that none of the locks are safe to use, and they should unlock the lockId.
func (*Client) SLock ¶
func (c *Client) SLock(ctx context.Context, resourceName, lockId string, ld LockDetails, maxConcurrent int) error
SLock creates a shared lock on a resource and associates it with the provided lockId. Additional details about the lock can be supplied via LockDetails.
The maxConcurrent argument is used to limit the number of shared locks that can exist on a resource concurrently. When SLock is called with maxCurrent = N, the lock request will fail unless there are less than N shared locks on the resource already. If maxConcurrent is negative then there is no limit to the number of shared locks that can exist.
func (*Client) Status ¶
Status returns the status of locks that match the provided filter. Fields with zero values in the Filter struct are ignored.
func (*Client) Unlock ¶
Unlock unlocks all locks with the associated lockId. If there are multiple locks with the given lockId, they will be unlocked in the reverse order in which they were created in (newest to oldest). For every lock that is unlocked, a LockStatus struct (representing the lock before it was unlocked) is returned. The order of these is the order in which they were unlocked.
An error will only be returned if there is an issue unlocking a lock; an error will not be returned if a lock does not exist. If an error is returned, it is safe and recommended to retry this method until until there is no error.
type Filter ¶
type Filter struct { CreatedBefore time.Time // Only include locks created before this time. CreatedAfter time.Time // Only include locks created after this time. TTLlt uint // Only include locks with a TTL less than this value, in seconds. TTLgte uint // Only include locks with a TTL greater than or equal to this value, in seconds. Resource string // Only include locks on this resource. LockId string // Only include locks with this lockId. Owner string // Only include locks with this owner. }
Filter contains fields that are used to filter locks when querying their status. Fields with zero values are ignored.
type LockDetails ¶
type LockDetails struct { // The user that is creating the lock. Owner string // The host that the lock is being created from. Host string // The time to live (TTL) for the lock, in seconds. Setting this to 0 // means that the lock will not have a TTL. TTL uint }
LockDetails contains fields that are used when creating a lock.
type LockStatus ¶
type LockStatus struct { // The name of the resource that the lock is on. Resource string // The id of the lock. LockId string // The type of the lock ("exclusive" or "shared") Type string // The name of the user who created the lock. Owner string // The host that the lock was created from. Host string // The time that the lock was created at. CreatedAt time.Time // The time that the lock was renewed at, if applicable. RenewedAt *time.Time // The TTL for the lock, in seconds. A negative value means that the // lock does not have a TTL. TTL int64 // contains filtered or unexported fields }
LockStatus represents the status of a lock.
type LockStatusesByCreatedAtDesc ¶
type LockStatusesByCreatedAtDesc []LockStatus
LockStatusesByCreatedAtDesc is a slice of LockStatus structs, ordered by CreatedAt descending.
func (LockStatusesByCreatedAtDesc) Len ¶
func (ls LockStatusesByCreatedAtDesc) Len() int
func (LockStatusesByCreatedAtDesc) Less ¶
func (ls LockStatusesByCreatedAtDesc) Less(i, j int) bool
func (LockStatusesByCreatedAtDesc) Swap ¶
func (ls LockStatusesByCreatedAtDesc) Swap(i, j int)
type Purger ¶
type Purger interface { // Purge deletes expired locks. It is important to note that not just // locks with a TTL of 0 get deleted - any lock that shares a lockId // with a lock that has has a TTL of 0 will be removed. Purge(ctx context.Context) ([]LockStatus, error) }
A Purger deletes expired locks.