setddblock

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 18 Imported by: 0

README

setddblock

Latest GitHub release Github Actions test Go Report Card License Documentation

setddblock is setlock like command line tool with AWS DynamoDB

Usage

$ setddblock -xN ddb://ddb_lock_table/lock_item_id your_command
Usage: setddblock [ -nNxX ] [--endpoint <endpoint>] [--debug --version] ddb://<table_name>/<item_id> your_command
Flags:
  -n
        No delay. If fn is locked by another process, setlock gives up.
  -N
        (Default.) Delay. If fn is locked by another process, setlock waits until it can obtain a new lock.
  -x
        If fn cannot be update-item (or put-item) or locked, setlock exits zero.
  -X
        (Default.) If fn cannot be update-item (or put-item) or locked, setlock prints an error message and exits nonzero.
  --debug
        show debug log
  --endpoint string
        If you switch remote, set AWS DynamoDB endpoint url.
  --region string
        aws region
  --timeout string
        set command timeout
  --version
        show version

the required IAM Policy is as follows:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "dynamodb:CreateTable",
                "dynamodb:UpdateTimeToLive",
                "dynamodb:PutItem",
                "dynamodb:DeleteItem",
                "dynamodb:DescribeTable",
                "dynamodb:GetItem",
                "dynamodb:UpdateItem"
            ],
            "Resource": "*"
        }
    ]
}

If the lock table has already been created, dynamodb:CreateTable and dynamodb:UpdateTimeToLive are not required.

Install

binary packages

Releases.

Homebrew tap
$ brew install mashiike/tap/setddblock

Usage as a library

setddblock.New(url string, optFns ...func(*setddblock.Options)) returns a DynamoDBLocker that satisfies the sync.Locker interface.

l, err := setddblock.New("ddb://ddb_lock_table/lock_item_id")
if err != nil {
	// ...
}
func () {
    l.Lock()
    defer l.Unlock()
    // ...
}()

Note: If Lock or Unlock fails, for example because you can't connect to DynamoDB, it will panic.
If you don't want it to panic, use LockWithError() and UnlockWithErr(). Alternatively, use the WithNoPanic option.

more infomation see go doc.

License

see LICENSE file.

Documentation

Overview

Package setddblock provides a distributed lock mechanism using DynamoDB.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLeaseDuration = 10 * time.Second
)

Default values

Functions

func Recover

func Recover(e interface{}) error

Recover for Lock() and Unlock() panic

func WithContext added in v0.1.0

func WithContext(ctx context.Context) func(opts *Options)

WithContext specifies the Context used by Lock() and Unlock().

func WithDelay

func WithDelay(delay bool) func(opts *Options)

WithDelay will delay the acquisition of the lock if it fails to acquire the lock. This is similar to the N option of setlock. The default is delay enabled (true). Specify false if you want to exit immediately if Lock acquisition fails.

func WithEndpoint

func WithEndpoint(endpoint string) func(opts *Options)

WithEndpoint is an endpoint specification option for Local development. Please enter the URL of DynamoDB Local etc.

func WithExpireGracePeriod added in v0.6.0

func WithExpireGracePeriod(d time.Duration) func(opts *Options)

WithExpireGracePeriod specifies the grace period after the lease expires during which the lock can still be reclaimed.

If the grace period is greater than zero, the lock may be forcibly reacquired once both the lease has expired and the specified grace period has elapsed.

If the grace period is zero or negative, automatic reclamation is disabled; expired locks will remain until removed by DynamoDB's TTL mechanism.

func WithLeaseDuration

func WithLeaseDuration(d time.Duration) func(opts *Options)

WithLeaseDuration affects the heartbeat interval and TTL after Lock acquisition. The default is 10 seconds

func WithLogger

func WithLogger(logger *slog.Logger) func(opts *Options)

WithLogger is a setting to enable the log output of DynamoDB Locker. By default, Logger that does not output anywhere is specified.

func WithNoPanic

func WithNoPanic() func(opts *Options)

WithNoPanic changes the behavior so that it does not panic if an error occurs in the Lock () and Unlock () functions. Check the LastErr () function to see if an error has occurred when WithNoPanic is specified.

func WithRegion

func WithRegion(region string) func(opts *Options)

WithRegion specifies the AWS Region. Default AWS_DEFAULT_REGION env

Types

type DynamoDBLocker

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

DynamoDBLocker implements the sync.Locker interface and provides a Lock mechanism using DynamoDB.

func New

func New(urlStr string, optFns ...func(*Options)) (*DynamoDBLocker, error)

New returns *DynamoDBLocker

func (*DynamoDBLocker) ClearLastErr added in v0.3.0

func (l *DynamoDBLocker) ClearLastErr()

func (*DynamoDBLocker) LastErr added in v0.3.0

func (l *DynamoDBLocker) LastErr() error

func (*DynamoDBLocker) Lock

func (l *DynamoDBLocker) Lock()

Lock for implements sync.Locker

func (*DynamoDBLocker) LockWithErr

func (l *DynamoDBLocker) LockWithErr(ctx context.Context) (bool, error)

LockWithErr try get lock. The return value of bool indicates whether Lock has been released. If true, it is Lock Granted.

func (*DynamoDBLocker) Unlock

func (l *DynamoDBLocker) Unlock()

Unlock for implements sync.Locker

func (*DynamoDBLocker) UnlockWithErr

func (l *DynamoDBLocker) UnlockWithErr(ctx context.Context) error

UnlockWithErr unlocks. Delete DynamoDB items

type Options

type Options struct {
	NoPanic           bool
	Logger            *slog.Logger
	Delay             bool
	Endpoint          string
	Region            string
	LeaseDuration     time.Duration
	ExpireGracePeriod time.Duration
	// contains filtered or unexported fields
}

Options are for changing the behavior of DynamoDB Locker and are changed by the function passed to the New () function. See the WithXXX options for more information.

Directories

Path Synopsis
cmd
setddblock command

Jump to

Keyboard shortcuts

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