Documentation
¶
Index ¶
- Constants
- Variables
- type Lock
- type LockManager
- type Option
- func WithBucketDescription(desc string) Option
- func WithBucketName(name string) Option
- func WithBucketReplicas(replicas int) Option
- func WithKeepAlive(enable bool) Option
- func WithKeepAliveIntervalFactor(factor int) Option
- func WithLogger(logger *slog.Logger) Option
- func WithRetryInterval(interval time.Duration) Option
- func WithTTL(ttl time.Duration) Option
- type Options
Constants ¶
const ( // DefaultLockTTL is the default time-to-live for a lock key. DefaultLockTTL = 30 * time.Second // DefaultRetryInterval is the default interval to wait before retrying acquisition. DefaultRetryInterval = 250 * time.Millisecond // DefaultKeepAliveIntervalFactor is the factor of TTL used for the keep-alive interval (TTL / factor). // A factor of 3 means the keep-alive will run at 1/3 of the TTL duration. DefaultKeepAliveIntervalFactor = 3 )
Variables ¶
var ( // ErrLockAcquireTimeout is returned when acquiring a lock times out. ErrLockAcquireTimeout = errors.New("lock acquisition timed out") // ErrLockNotHeld is returned when trying to release or refresh a lock that is not held or lost. ErrLockNotHeld = errors.New("lock not held or already released/expired") // ErrLockAlreadyLocked is a specific error type when creation fails because the key exists. ErrLockAlreadyLocked = errors.New("lock key already exists") // ErrLockHeld is returned by TryAcquire when the lock is currently held by another owner. ErrLockHeld = errors.New("lock already held") )
Functions ¶
This section is empty.
Types ¶
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock represents an acquired distributed lock.
type LockManager ¶
type LockManager struct {
// contains filtered or unexported fields
}
LockManager manages distributed locks using a NATS KV bucket.
func NewLockManager ¶
func NewLockManager(ctx context.Context, js jetstream.JetStream, opts ...Option) (*LockManager, error)
NewLockManager creates a new LockManager. It ensures the necessary NATS KV bucket exists with the configured TTL.
func (*LockManager) Acquire ¶
Acquire attempts to acquire a lock for the given key. It blocks until the lock is acquired or the context is cancelled/times out. Returns the acquired Lock or an error.
func (*LockManager) Do ¶
func (m *LockManager) Do(ctx context.Context, key string, fn func(ctx context.Context) error) (executed bool, err error)
Do executes the provided function 'fn' only if the lock for 'key' can be acquired immediately.
Use this in clustered environments where multiple replicas run the same code, but you only want one of them to execute the logic.
Returns: - executed (bool): true if the lock was acquired and fn was called, false if lock was busy. - err (error): contains errors from NATS (during acquisition) or errors returned by 'fn'.
func (*LockManager) TryAcquire ¶
TryAcquire attempts to acquire the lock exactly once without retrying. If the lock is available, it returns the Lock object. If the lock is already held, it returns ErrLockHeld. If a network/infra error occurs, it returns that error.
type Option ¶
type Option func(*Options)
Option is a function type for setting LockManager options.
func WithBucketDescription ¶
WithBucketDescription sets the description for the KV bucket.
func WithBucketName ¶
WithBucketName sets the name of the NATS KV bucket used for locks. Defaults to "distributed_locks".
func WithBucketReplicas ¶
WithBucketReplicas sets the number of replicas for the lock bucket. Defaults to 1. Only relevant in clustered NATS setups.
func WithKeepAlive ¶
WithKeepAlive enables or disables automatic background refresh (keep-alive) for acquired locks. Disable this if you manually manage lock refresh/release. Defaults to true.
func WithKeepAliveIntervalFactor ¶
WithKeepAliveIntervalFactor sets the factor of the lock TTL to use for the keep-alive interval. The interval is calculated as TTL / factor. A smaller factor means more frequent refreshes. Defaults to 3. Must be 2 or greater.
func WithLogger ¶
WithLogger sets a custom slog logger. Defaults to slog.Default().
func WithRetryInterval ¶
WithRetryInterval sets the base interval between lock acquisition attempts. A small amount of random jitter will be applied to this interval to prevent thundering herds.
type Options ¶
type Options struct {
TTL time.Duration
RetryInterval time.Duration
KeepAlive bool // Enable automatic keep-alive for acquired locks
KeepAliveIntervalFactor int // Factor of TTL for keep-alive interval (TTL / factor)
Logger *slog.Logger
BucketName string
BucketDescription string
BucketReplicas int
}
Options configure the LockManager and lock acquisition.