Documentation
¶
Overview ¶
Package lease provides a small, correctness-first DynamoDB lease/lock helper.
It is designed for ISR-style regeneration locks (FaceTheory) and similar distributed coordination needs.
Lease expiration is stored and compared in whole Unix seconds. A successful Acquire or Refresh rounds the expiry up to the whole second at or after now+duration, so a lease is never eligible for takeover before the requested positive duration has elapsed; whole-second clocks and durations are unchanged.
Index ¶
- Constants
- func IsLeaseHeld(err error) bool
- func IsLeaseNotOwned(err error) bool
- type DynamoDBLeaseAPI
- type Key
- type Lease
- type LeaseHeldError
- type LeaseNotOwnedError
- type Manager
- func (m *Manager) Acquire(ctx context.Context, pk string, duration time.Duration) (*Lease, error)
- func (m *Manager) AcquireKey(ctx context.Context, key Key, duration time.Duration) (*Lease, error)
- func (m *Manager) Refresh(ctx context.Context, lease Lease, duration time.Duration) (*Lease, error)
- func (m *Manager) Release(ctx context.Context, lease Lease) error
- type Option
- func WithIncludeTTL(include bool) Option
- func WithKeyAttributeNames(pkAttr, skAttr string) Option
- func WithLeaseAttributeNames(tokenAttr, expiresAtAttr, ttlAttr string) Option
- func WithLockSortKey(lockSortKey string) Option
- func WithNow(now func() time.Time) Option
- func WithTTLBuffer(buffer time.Duration) Option
- func WithTokenGenerator(token func() string) Option
- func WithValidateInput(validate bool) Option
Constants ¶
const ( DefaultPKAttribute = "pk" DefaultSKAttribute = "sk" DefaultTokenAttribute = "lease_token" DefaultExpiresAtAttribute = "lease_expires_at" DefaultTTLAttribute = "ttl" DefaultLockSortKey = "LOCK" DefaultTTLBuffer = time.Hour )
Variables ¶
This section is empty.
Functions ¶
func IsLeaseHeld ¶
func IsLeaseNotOwned ¶
Types ¶
type DynamoDBLeaseAPI ¶
type DynamoDBLeaseAPI interface {
PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
}
type LeaseHeldError ¶
type LeaseHeldError struct {
Key Key
}
LeaseHeldError indicates a lease could not be acquired because it is held by another contender. This is the "expected" failure mode for lock contention.
func (*LeaseHeldError) Error ¶
func (e *LeaseHeldError) Error() string
type LeaseNotOwnedError ¶
type LeaseNotOwnedError struct {
Key Key
}
LeaseNotOwnedError indicates a refresh attempt failed because the caller's token no longer owns the lease. This can happen if the lease expired and was acquired by another contender, or if the token is wrong.
func (*LeaseNotOwnedError) Error ¶
func (e *LeaseNotOwnedError) Error() string
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
func NewManager ¶
func NewManager(client DynamoDBLeaseAPI, tableName string, opts ...Option) (*Manager, error)
func (*Manager) AcquireKey ¶
type Option ¶
type Option func(*Manager)