From e1dff8293821c2e01cc79a3a3bac4e07603ff295 Mon Sep 17 00:00:00 2001 From: Luis De Anda Date: Sun, 25 Dec 2022 11:27:30 -0600 Subject: [PATCH 1/4] Install `p-limit` package --- package-lock.json | 11 ++--------- package.json | 1 + 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index c3632d0..b0f0591 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "asl-validator": "^3.0.8", "jsonpath-plus": "^6.0.1", "lodash": "^4.17.21", + "p-limit": "^3.1.0", "wildcard-match": "^5.1.2" }, "devDependencies": { @@ -5472,8 +5473,6 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", - "dev": true, - "peer": true, "dependencies": { "yocto-queue": "^0.1.0" }, @@ -6542,8 +6541,6 @@ "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", - "dev": true, - "peer": true, "engines": { "node": ">=10" }, @@ -10887,8 +10884,6 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", - "dev": true, - "peer": true, "requires": { "yocto-queue": "^0.1.0" } @@ -11626,9 +11621,7 @@ "yocto-queue": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", - "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", - "dev": true, - "peer": true + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==" } } } diff --git a/package.json b/package.json index 7be08af..8c8fac4 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "asl-validator": "^3.0.8", "jsonpath-plus": "^6.0.1", "lodash": "^4.17.21", + "p-limit": "^3.1.0", "wildcard-match": "^5.1.2" } } From 40b29fc75c0781af0f3fbea88034c6eafaa9b751 Mon Sep 17 00:00:00 2001 From: Luis De Anda Date: Sun, 25 Dec 2022 12:36:10 -0600 Subject: [PATCH 2/4] Add support for `MaxConcurrency` in `Map` state --- src/StateMachine.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/StateMachine.ts b/src/StateMachine.ts index 6733008..b8a22e0 100644 --- a/src/StateMachine.ts +++ b/src/StateMachine.ts @@ -16,6 +16,7 @@ import { testChoiceRule } from './ChoiceHelper'; import aslValidator from 'asl-validator'; import set from 'lodash/set.js'; import cloneDeep from 'lodash/cloneDeep.js'; +import pLimit from 'p-limit'; export class StateMachine { /** @@ -302,14 +303,11 @@ export class StateMachine { return; } - const result = new Array(items.length); - let paramValue; - for (let i = 0; i < items.length; i++) { - const item = items[i]; - + const processItem = (item: JSONValue, index: number): Promise => { + let paramValue; this.context['Map'] = { Item: { - Index: i, + Index: index, Value: item, }, }; @@ -321,8 +319,12 @@ export class StateMachine { // Pass the current parameter value if defined, otherwise pass the current item being iterated const mapStateMachine = new StateMachine(state.Iterator, this.validationOptions); - result[i] = await mapStateMachine.run(paramValue ?? item, options); - } + return mapStateMachine.run(paramValue ?? item, options); + }; + + const limit = pLimit(state.MaxConcurrency || 40); // If `MaxConcurrency` is 0 or not specified, default to running 40 iterations concurrently + const input = items.map((item, i) => limit(() => processItem(item, i))); + const result = await Promise.all(input); delete this.context['Map']; this.currResult = result; From 3606b0c7b053ecead410c2acfb0c10e950a7148a Mon Sep 17 00:00:00 2001 From: Luis De Anda Date: Sun, 25 Dec 2022 12:45:08 -0600 Subject: [PATCH 3/4] Move magic value into constant --- src/StateMachine.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/StateMachine.ts b/src/StateMachine.ts index b8a22e0..7ac1aaa 100644 --- a/src/StateMachine.ts +++ b/src/StateMachine.ts @@ -322,7 +322,8 @@ export class StateMachine { return mapStateMachine.run(paramValue ?? item, options); }; - const limit = pLimit(state.MaxConcurrency || 40); // If `MaxConcurrency` is 0 or not specified, default to running 40 iterations concurrently + const DEFAULT_MAX_CONCURRENCY = 40; // If `MaxConcurrency` is 0 or not specified, default to running 40 iterations concurrently + const limit = pLimit(state.MaxConcurrency || DEFAULT_MAX_CONCURRENCY); const input = items.map((item, i) => limit(() => processItem(item, i))); const result = await Promise.all(input); From f77b0347b68cbcd20c45200e14b1c2a6e99a8d41 Mon Sep 17 00:00:00 2001 From: Luis De Anda Date: Sun, 25 Dec 2022 13:49:34 -0600 Subject: [PATCH 4/4] Update feature support --- docs/feature-support.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/feature-support.md b/docs/feature-support.md index 4c186e6..28c238a 100644 --- a/docs/feature-support.md +++ b/docs/feature-support.md @@ -13,6 +13,10 @@ - [x] Pass - [x] Wait - [x] Succeed + - [x] Map + - [x] `Iterator` + - [x] `ItemsPath` + - [x] `MaxConcurrency` ## Limited support @@ -23,10 +27,6 @@ - [ ] `HeartbeatSeconds` - [ ] `TimeoutSecondsPath` - [ ] `HeartbeatSecondsPath` - - Map - - [x] `Iterator` - - [x] `ItemsPath` - - [ ] `MaxConcurrency` - Choice - Boolean expressions - [x] `And`