Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Custom Errors ErrLockNotHeld = errors.New("lock not held") ErrLockAcquisitionFailed = errors.New("failed to acquire lock") )
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend interface { // WriteEntry attempts to create or update a lock entry in the storage. // It returns true if the lock was obtained, false if not, or an error if something unexpected happened. WriteEntry(ctx context.Context, key, nonce string, lease time.Duration) (bool, error) // DelEntry attempts to delete a lock entry from the storage. // It returns true if the lock was released, false if not, or an error if something unexpected happened. DelEntry(ctx context.Context, key, nonce string) (bool, error) }
Backend defines the interface for the underlying storage backend of the distributed lock.
type Dlock ¶
type Dlock struct { ID uint // The unique identifier for the lock. Key string `gorm:"index:uidx_key,unique;not null"` // The secret used to release the lock. Nonce string `gorm:"not null"` // Version number incremented with each update. Version uint `gorm:"default:0;not null"` // When the lock expires. ExpiredAt time.Time `gorm:"not null"` // CRUD timestamp. CreatedAt, UpdatedAt time.Time }
Dlock represents a distributed lock.
type LockIntent ¶
type LockIntent struct { Key string // The unique identifier for the lock. Nonce string // The lock can only be released with the correct nonce. Lease time.Duration // The length of the lease duration. }
The lock intent to acquire a distributed lock.
func NewLockIntent ¶
func NewLockIntent(key, nonce string, lease time.Duration) *LockIntent
type LockManager ¶
type LockManager struct {
// contains filtered or unexported fields
}
LockManager manages distributed locks using a backend storage system.
func NewLockManager ¶
func NewLockManager(be Backend) *LockManager
NewLockManager creates a new LockManager with the provided backend.
func NewLockManagerFromViper ¶
func NewLockManagerFromViper() *LockManager
func (*LockManager) Acquire ¶
func (l *LockManager) Acquire(ctx context.Context, li *LockIntent) error
Acquire tries to acquire a distributed lock with the given key and lease duration. It returns nil error if the lease was successfully obtained.
func (*LockManager) Release ¶
func (l *LockManager) Release(ctx context.Context, li *LockIntent) error
Release attempts to release an existing lock. It returns nil error if the lock was successfully released.
type MySQLBackend ¶
type MySQLBackend struct {
// contains filtered or unexported fields
}
MySQLBackend provides MySQL implementation for Backend interface.
func NewMySQLBackend ¶
func NewMySQLBackend(db *gorm.DB) *MySQLBackend
NewMySQLBackend creates a new instance of MySQLBackend.
func (*MySQLBackend) WriteEntry ¶
func (m *MySQLBackend) WriteEntry( ctx context.Context, key, nonce string, lease time.Duration) (bool, error)
Implement WriteEntry - try to acquire a lock