distrlock

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLockUpdate = errors.New("lock update error")
View Source
var ErrTimeout = errors.New("timeout")
View Source
var SkString = &types.AttributeValueMemberS{Value: "##LOCK##"}

Functions

func WithRefreshInterval

func WithRefreshInterval(refreshInterval time.Duration) func(options *Options)

WithRefreshInterval Specifies a refresh interval that is used by the lock handler to poll if the lock is still active. Default value is 200ms

func WithSortKey

func WithSortKey(sortKeyColumnName string) func(options *Options)

WithSortKey Specifies the sort key column if exists.

func WithSortKeyValue

func WithSortKeyValue(sortKeyValue types.AttributeValue) func(options *Options)

WithSortKeyValue Specifies the sort key value to use for a lock. This is required if the column hash a sort key that is not of they string. Note that the partition key + sort key must be unique within the table.

func WithTimeout

func WithTimeout(timeout time.Duration) func(options *Options)

WithTimeout Specifies a timeout value set in the distributed lock. Default value is 1 second.

Types

type DynamodbClient

type DynamodbClient interface {
	PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(options *dynamodb.Options)) (*dynamodb.PutItemOutput, error)
	GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(options *dynamodb.Options)) (*dynamodb.GetItemOutput, error)
	DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(options *dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
}

type IdGenerator

type IdGenerator interface {
	ID() string
}

type Lock

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

func (*Lock) LockId

func (l *Lock) LockId() string

LockId returns the current id used by the lock

func (*Lock) Refresh

func (l *Lock) Refresh(ctx context.Context) error

Refresh updates the timeout of the current active lock

func (*Lock) Release

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

Release remove the lock in the database

func (*Lock) TransactionCondition

func (l *Lock) TransactionCondition() types.TransactWriteItem

TransactionCondition returns a TransactWriteItem to validate if the lock is still active

func (*Lock) TransactionWithRefresh

TransactionWithRefresh returns a TransactWriteItem to validate if the lock is still active and refresh the lock if successful Note the callback function returned as second argument should be called with the return types of the TransactWriteItems call

type Options

type Options struct {
	// SortKeyName if table has a sort key this value represent the sort key value
	SortKeyName *string

	// SortKeyDefault value
	SortKeyValue types.AttributeValue

	// Timeout stored in distributed lock
	Timeout *time.Duration

	// RefreshInterval between two lock poll request
	RefreshInterval *time.Duration

	// IdGenerator a generator of unique IDs
	IdGenerator IdGenerator
}

type RepositoryLockHandler

type RepositoryLockHandler struct {
	Client           DynamodbClient
	TableName        string
	PartitionKeyName string
	SortKeyName      *string
	SortKeyValue     types.AttributeValue
	Timeout          time.Duration
	RefreshInterval  time.Duration
	IdGenerator      IdGenerator
}

RepositoryLockHandler will handle locks distributed locks within a single dynamodb table The RepositoryLockHandler can create locks on hash tables and hash+range tables

func New

func New(client DynamodbClient, tableName string, partitionKeyName string, optFns ...func(options *Options)) *RepositoryLockHandler

New create a new initialized distributed lock.

func (*RepositoryLockHandler) Lock

func (h *RepositoryLockHandler) Lock(ctx context.Context, partition types.AttributeValue) (*Lock, error)

Lock tries to lock a specified partition. The method will return a new lock once it is able to create a lock. If it was unable to create a new lock it will try after a RefreshInterval. Polling will stop if the context is Done.

func (*RepositoryLockHandler) TryLock

func (h *RepositoryLockHandler) TryLock(ctx context.Context, partition types.AttributeValue) (*Lock, bool, error)

TryLock tries to lock a specified partition. If the handler was able to lock the partition a new lock will be returned. Additionally, the second return argument will be true If the handler was unable to lock the partition nil and false is returned as first arguments.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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