setddblock is setlock like command line tool with AWS DynamoDB
$ setddblock -xN ddb://ddb_lock_table/lock_item_id your_commandUsage: 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 (e.g., 30s, 1m, 2h)
--version
show versionthe 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.
$ brew install mashiike/tap/setddblocksetddblock.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.
The setddblock tool now supports TTL (Time-To-Live) expiration for locks. This feature ensures that locks are automatically released after a specified duration, preventing stale locks from persisting indefinitely. If setddblock isn't run before the TTL expires, DynamoDB will eventually purge the stale item.
- When a lock is acquired, a TTL is set on the lock item in DynamoDB.
- If a locked process dies and heartbeats stop updating the TTL, the lock will automatically expire and be released after the TTL duration, allowing other processes to acquire the lock.
- The TTL is automatically calculated based on the lease duration set with the
WithLeaseDurationoption. - The TTL attribute is automatically set when the lock table is created and is updated by the heartbeat of a running locked process.
For more information, see go doc.
see LICENSE file.