memcache

package module
v2.0.0-...-1948240 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

type Locker struct {
	// contains filtered or unexported fields
}

Locker is a distributed lock.

It is a renewable distributed lock built on memcached: each lock maps to one memcached key and its ownership is defined by an independent version identifier. While the lock is held its expiration is refreshed through a "read-verify-CAS" loop, and the lock becomes invalid once it expires or its ownership is lost.

func (*Locker) Acquire

func (l *Locker) Acquire(ctx context.Context) error

Acquire acquires the lock.

Acquire blocks until the lock is acquired, after which it starts a background renewal to keep holding the lock until it is explicitly released or lost. The ctx is the context whose cancellation aborts the acquisition. It returns nil on success, errors.ErrDeadlineExceeded when the retries are exhausted, or ctx.Err() when the context is canceled.

func (*Locker) Release

func (l *Locker) Release(ctx context.Context) error

Release releases the lock.

Release stops the background renewal first and then releases the lock by version identifier; only the owner of the lock (a matching version identifier) can release it successfully. The ctx is the context to use. It returns nil on success, errors.ErrIllegalOperation when the lock has been lost or its ownership has changed, or the driver error on a persistent CAS conflict (a transient race).

func (*Locker) TryAcquire

func (l *Locker) TryAcquire(ctx context.Context, expiration ...time.Duration) error

TryAcquire attempts to acquire the lock.

TryAcquire makes a single attempt and returns immediately on failure without blocking. On success, a positive expiration makes the lock expire once after that duration without renewal; otherwise the lock uses the default expiration and starts a background renewal. The ctx is the context to use. The optional expiration is a fixed expiration time; when it is empty or not positive, the lock is not time-bounded and renewal is enabled. It returns nil on success or errors.ErrIllegalOperation when the lock is already held by someone else.

type Maker

type Maker struct {
	// contains filtered or unexported fields
}

Maker is a lock maker.

It creates distributed locks from the memcached Add/Get/CompareAndSwap primitives. It holds the memcached client and the global lock configuration internally, and generates an independent version identifier for each lock to verify lock ownership.

func NewMaker

func NewMaker(opts ...Option) *Maker

NewMaker creates a lock maker.

It initializes the lock configuration: when the expiration is not explicitly configured the default value is used, and an expiration below 1 second is clamped to 1 second so that memcached does not truncate a sub-second expiration to 0 (never expires). An acquisition interval of 0 or less is clamped to the default so that the retry loop does not degenerate into a busy-wait without backoff. When no external client is provided, a built-in client is created automatically and is closed along with Close.

func (*Maker) Close

func (m *Maker) Close() error

Close closes the maker.

It first cancels the maker lifecycle context to stop every background renewal goroutine, then closes the built-in client. When an external client is used, its lifecycle is managed by the external caller and is left untouched here.

func (*Maker) Make

func (m *Maker) Make(name string) lock.Locker

Make makes a Locker.

It builds the memcached key by concatenating the configured prefix with the lock name, and generates a unique version identifier for the lock. Every Locker holds an independent version identifier so that they can fairly compete for the same lock.

type Option

type Option func(o *options)

Option is a lock configuration function.

func WithAcquireInterval

func WithAcquireInterval(acquireInterval time.Duration) Option

WithAcquireInterval sets the interval between lock acquisition attempts.

It is the interval between retries after a failed lock acquisition; a value of 0 or less is clamped to the default 20ms in NewMaker.

func WithAcquireMaxRetries

func WithAcquireMaxRetries(acquireMaxRetries int) Option

WithAcquireMaxRetries sets the maximum number of lock acquisition retries.

When the maximum number of retries is reached without acquiring the lock, a timeout error is returned; 0 means unlimited retries until success or context cancellation.

func WithAddrs

func WithAddrs(addrs ...string) Option

WithAddrs sets the client connection addresses.

It sets the connection addresses of the built-in client and takes effect only when no external client is specified.

func WithClient

func WithClient(client *memcache.Client) Option

WithClient sets an external client.

It sets an external client, which is preferred when present; in that case the Maker no longer manages the client's lifecycle when it is closed.

func WithExpiration

func WithExpiration(expiration time.Duration) Option

WithExpiration sets the lock expiration.

It sets the expiration of the lock, which is also the target duration refreshed by the background renewal. memcached has second granularity, so a value below 1s is clamped to 1s. Note: memcached records the expiration in whole seconds, so the actual lifetime may be nearly one second shorter than the configured value. The background renewal interval is half of the expiration, so an expiration below 2s may make it hard for the renewal to finish before the lock expires; for a renewal scenario that needs to hold the lock continuously, a value of at least 3s is recommended.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets the key prefix.

The final lock key is prefix + ":" + name; when the prefix is empty no concatenation happens.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL