Create distributed semaphores using AWS Step Functions and Amazon DynamoDB to control concurrent invocations of contentious work.
Project description
@dontirun/state-machine-semaphore
An aws-cdk construct that enables you to use AWS Step Functions to control concurrency in your distributed system. You can use this construct to distributed state machine semaphores to control concurrent invocations of contentious work.
This construct is based off of Justin Callison's example code. Make sure to check out Justin's blogpost to learn about how the system works.
Examples
Example 1) A state machine with a controlled job
Click to see code
import { Function } from 'aws-cdk-lib/aws-lambda';
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { StateMachine, Succeed, Wait, WaitTime } from 'aws-cdk-lib/aws-stepfunctions';
import { LambdaInvoke } from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { Construct } from 'constructs';
import { Semaphore } from '@dontirun/state-machine-semaphore';
export class CdkTestStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const contestedJob = new LambdaInvoke(this, 'ContestedJobPart1', {
lambdaFunction: Function.fromFunctionName(this, 'JobFunctionPart1', 'cool-function'),
}).next(new Wait(this, 'Wait', { time: WaitTime.duration(Duration.seconds(7)) }))
.next(new Wait(this, 'AnotherWait', { time: WaitTime.duration(Duration.seconds(7)) }))
.next(new Wait(this, 'YetAnotherWait', { time: WaitTime.duration(Duration.seconds(7)) }));
const afterContestedJob = new Succeed(this, 'Succeed');
const stateMachineFragment = new Semaphore(stack, 'Semaphore', { lockName: 'life', limit: 42, job: contestedJob, nextState: afterContestedJob });
new StateMachine(this, 'StateMachine', {
definition: stateMachineFragment,
});
}
}
Click to see the state machine definition
Example 2) A state machine with multiple semaphores
Click to see code
import { Function } from 'aws-cdk-lib/aws-lambda';
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { StateMachine, Succeed, Wait, WaitTime } from 'aws-cdk-lib/aws-stepfunctions';
import { LambdaInvoke } from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { Construct } from 'constructs';
import { Semaphore } from '@dontirun/state-machine-semaphore';
export class CdkTestStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const contestedJob = new LambdaInvoke(this, 'ContestedJobPart1', {
lambdaFunction: Function.fromFunctionName(this, 'JobFunctionPart1', 'cool-function'),
})
const notContestedJob = new LambdaInvoke(this, 'NotContestedJob', {
lambdaFunction: Function.fromFunctionName(this, 'NotContestedJobFunction', 'cooler-function'),
})
const contestedJob2 = new LambdaInvoke(this, 'ContestedJobPart2', {
lambdaFunction: Function.fromFunctionName(this, 'JobFunctionPart2', 'coolest-function'),
})
const afterContestedJob2 = new Succeed(this, 'Succeed');
const definition = new Semaphore(stack, 'Semaphore', { lockName: 'life', limit: 42, job: contestedJob, nextState: notContestedJob })
.next(new Semaphore(stack, 'Semaphore2', { lockName: 'liberty', limit: 7, job: contestedJob2, nextState: afterContestedJob2 }));
new StateMachine(this, 'StateMachine', {
definition: definition,
});
}
}
Click to see the state machine definition
API Reference
See API.md.
License
This project is licensed under the Apache-2.0 License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
state-machine-semaphore-0.1.1.tar.gz
(326.9 kB
view hashes)
Built Distribution
Close
Hashes for state-machine-semaphore-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82efcdb639f8c9c19b1a15dd411c351b0578169443e3e7b5fef5d11ce1f8d32b |
|
MD5 | dc8164ec010d9948ed8da6c460418a8d |
|
BLAKE2b-256 | 99101a6371edbd915729d2bd4700470cfd745041388ecbb150c2a9ed247dd6df |
Close
Hashes for state_machine_semaphore-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 736d0632bcac422321d30f9d02e7be73df79cdc4fa9523cd7dbeddc36f8c9fae |
|
MD5 | bf988715d4fdf62aafe1adff00b77190 |
|
BLAKE2b-256 | 3e9cd38e52264c68263d35a21979ae2386559b4b5c03003a19bc6a73bc7c0ab5 |