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
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 { SemaphoreGenerator } 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 generator = new SemaphoreGenerator(this, 'SemaphoreGenerator');
const stateMachineFragment = generator.generateSemaphoredJob('life', 42, contestedJob, afterContestedJob);
new StateMachine(this, 'StateMachine', {
definition: stateMachineFragment,
});
}
}
|
|
Example 2) A state machine with multiple semaphores
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 { SemaphoreGenerator } 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 generator = new SemaphoreGenerator(this, 'SemaphoreGenerator');
const stateMachineFragment = generator.generateSemaphoredJob('life', 42, contestedJob, notContestedJob);
const stateMachineFragment2 = generator.generateSemaphoredJob('liberty', 7, contestedJob2, afterContestedJob2);
new StateMachine(this, 'StateMachine', {
definition: stateMachineFragment.next(stateMachineFragment2),
});
}
}
|
|
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.0.0.tar.gz
(325.1 kB
view hashes)
Built Distribution
Close
Hashes for state-machine-semaphore-0.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9df8d3966def3ab50474d8841f92a65019af538c7fdc24b81787626d05b1ab1e |
|
MD5 | 153f2d87a498b0f5d628ca8b5b853c9c |
|
BLAKE2b-256 | f6f1fb1e26f55036f05a664064c6a4c2c03515149bd70345fd959c4cb81c3d2e |
Close
Hashes for state_machine_semaphore-0.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba5723fd9e76b5f2924dc0f16f98788e3dcd6f305cd7a0ec2edfbf65ebfd80ce |
|
MD5 | 5c5b906994a40067084f8fa4bf7711d3 |
|
BLAKE2b-256 | 9e4255b9473bb6900d55f99528faf1f101a6acd2d746e184209996c2f6fd1be0 |