Documentation
¶
Index ¶
- Variables
- type Config
- type DistributedLock
- type LockInfo
- type LockManager
- func (lm *LockManager) Cleanup(ctx context.Context) error
- func (lm *LockManager) Close() error
- func (lm *LockManager) NewDistributedLock(resource string, options ...LockOption) *DistributedLock
- func (lm *LockManager) StartCleanup(ctx context.Context) error
- func (lm *LockManager) StopCleanup()
- func (lm *LockManager) WithClock(clock clockwork.Clock) *LockManager
- func (lm *LockManager) WithLogger(logger *slog.Logger) *LockManager
- type LockOption
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLockAlreadyHeld is returned when attempting to acquire a lock that is already held by another node. ErrLockAlreadyHeld = errors.New("lock is already held by another node") // ErrLockNotHeld is returned when attempting to perform an operation on a lock that is not held by the current node. ErrLockNotHeld = errors.New("lock is not held by this node") // ErrLockExpired is returned when attempting to perform an operation on a lock that has expired. ErrLockExpired = errors.New("lock has expired") // ErrDatabaseError is returned when a database operation fails. ErrDatabaseError = errors.New("database operation failed") )
var DefaultConfig = Config{ MaxAttempts: 1, RetryDelay: 1 * time.Millisecond, HeartbeatInterval: 10 * time.Second, LeaseTime: 60 * time.Second, TablePrefix: "", SchemaName: "", CleanupInterval: 24 * time.Hour, InactivityThreshold: 12 * time.Hour, EnableAutomaticCleanup: true, }
DefaultConfig provides default configuration values.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// MaxAttempts is the maximum number of attempts to acquire the lock.
MaxAttempts int
// RetryDelay is the duration to wait between lock acquisition attempts.
RetryDelay time.Duration
// HeartbeatInterval is the duration between heartbeats to maintain the lock.
HeartbeatInterval time.Duration
// LeaseTime is the duration for which the lock is considered valid.
LeaseTime time.Duration
// TablePrefix is the prefix to use for database tables.
// For example, with TablePrefix "myapp_", the table will be "myapp_distributed_locks".
TablePrefix string
// SchemaName is the database schema to use.
SchemaName string
// CleanupInterval is the interval at which to run the cleanup process.
// Default is 24 hours.
CleanupInterval time.Duration
// InactivityThreshold is the duration of inactivity after which a lock is considered stale and can be cleaned up.
// Default is 12 hours.
InactivityThreshold time.Duration
// EnableAutomaticCleanup determines whether to automatically run the cleanup process.
// Default is true.
EnableAutomaticCleanup bool
}
Config holds the configuration options for the distributed lock system.
type DistributedLock ¶
type DistributedLock struct {
// contains filtered or unexported fields
}
DistributedLock represents a distributed lock implementation.
func (*DistributedLock) ExtendLease ¶
ExtendLease extends the lease time of the lock.
Parameters:
- ctx: The context for the operation.
- extension: The duration by which to extend the lease.
Returns:
- An error if the lease cannot be extended, nil otherwise.
func (*DistributedLock) IsLocked ¶
func (dl *DistributedLock) IsLocked(ctx context.Context) (bool, error)
IsLocked checks if the lock is currently held by this node.
Parameters:
- ctx: The context for the operation.
Returns:
- A boolean indicating whether the lock is held, and an error if the check fails.
func (*DistributedLock) Lock ¶
func (dl *DistributedLock) Lock(ctx context.Context) error
Lock attempts to acquire the distributed lock.
Parameters:
- ctx: The context for the operation.
Returns:
- An error if the lock cannot be acquired, nil otherwise.
- By default, no retries are performed unless configured differently.
type LockInfo ¶
type LockInfo struct {
NodeID uuid.UUID `json:"nodeID" db:"node_id"`
Resource string `json:"resource" db:"resource"`
ExpiresAt time.Time `json:"expiresAt" db:"expires_at"`
LastHeartbeat time.Time `json:"lastHeartbeat" db:"last_heartbeat"`
}
LockInfo holds information about a lock.
type LockManager ¶
type LockManager struct {
Config *Config
// contains filtered or unexported fields
}
LockManager is responsible for managing distributed locks in PostgreSQL.
func NewLockManager ¶
NewLockManager creates a new LockManager with the given configuration and database connection.
func (*LockManager) Cleanup ¶ added in v0.0.6
func (lm *LockManager) Cleanup(ctx context.Context) error
Cleanup removes locks that have been inactive for longer than the configured threshold. This method can be called manually if automatic cleanup is disabled.
func (*LockManager) Close ¶ added in v0.0.6
func (lm *LockManager) Close() error
Close releases resources held by the LockManager, including stopping the cleanup process.
func (*LockManager) NewDistributedLock ¶
func (lm *LockManager) NewDistributedLock(resource string, options ...LockOption) *DistributedLock
NewDistributedLock creates a new DistributedLock instance.
Parameters:
- resource: The name of the resource being locked.
- options: Optional parameters for customizing the lock behavior.
Returns:
- A pointer to the newly created DistributedLock.
func (*LockManager) StartCleanup ¶ added in v0.0.6
func (lm *LockManager) StartCleanup(ctx context.Context) error
StartCleanup starts the background process for cleaning up inactive locks. This method is automatically called by NewLockManager if EnableAutomaticCleanup is true.
func (*LockManager) StopCleanup ¶ added in v0.0.6
func (lm *LockManager) StopCleanup()
StopCleanup stops the background cleanup process.
func (*LockManager) WithClock ¶
func (lm *LockManager) WithClock(clock clockwork.Clock) *LockManager
WithClock sets a custom clock for the LockManager (mainly for testing purposes).
func (*LockManager) WithLogger ¶
func (lm *LockManager) WithLogger(logger *slog.Logger) *LockManager
WithLogger sets a custom logger for the LockManager.
type LockOption ¶
type LockOption func(*DistributedLock)
LockOption defines a function type for customizing a DistributedLock
func WithHeartbeatInterval ¶ added in v0.0.5
func WithHeartbeatInterval(heartbeatInterval time.Duration) LockOption
WithHeartbeatInterval sets a custom heartbeat interval for the lock
func WithLeaseTime ¶ added in v0.0.5
func WithLeaseTime(leaseTime time.Duration) LockOption
WithLeaseTime sets a custom lease time for the lock
func WithMaxAttempts ¶ added in v0.0.5
func WithMaxAttempts(maxAttempts int) LockOption
WithMaxAttempts sets a custom maximum number of attempts for lock acquisition
func WithRetryDelay ¶
func WithRetryDelay(retryDelay time.Duration) LockOption
WithRetryDelay sets a custom delay between retry attempts for lock acquisition