Skip to main content

Create distributed semaphores using AWS Step Functions and Amazon DynamoDB to control concurrent invocations of contentious work.

Project description

@dontirun/state-machine-semaphore

npm version PyPI version NuGet version Maven version Go version

View on Construct Hub

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 1 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

Example 2 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.360.tar.gz (329.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

state_machine_semaphore-0.1.360-py3-none-any.whl (327.6 kB view details)

Uploaded Python 3

File details

Details for the file state-machine-semaphore-0.1.360.tar.gz.

File metadata

File hashes

Hashes for state-machine-semaphore-0.1.360.tar.gz
Algorithm Hash digest
SHA256 0666498051684ca1f4f46c819ba1a2b645fd8dcc118f8ae62f959780c50132f3
MD5 2e6e7c2e7032396aca3e947290effc8f
BLAKE2b-256 f215d0fd5247eacd1cccdf5ff61441bf290d0fb250482c13279bdc1d5636d0d9

See more details on using hashes here.

File details

Details for the file state_machine_semaphore-0.1.360-py3-none-any.whl.

File metadata

File hashes

Hashes for state_machine_semaphore-0.1.360-py3-none-any.whl
Algorithm Hash digest
SHA256 ef337d738f7c1ac9df2913ff65f7d0b7cc22510f01a9c069ae777a7315a2996c
MD5 08984e84619a65055496d7aac161fa7c
BLAKE2b-256 f0e1cc3d6776c29eb054bc5af9d36f8aaa2d61b545a2bf786af6716d500c5947

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page