Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge pull request #417 from atomist/automation-client
Introduce promise pool to limit number of concurrent executions
- Loading branch information
Showing
6 changed files
with
128 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { configurationValue } from "../configuration"; | ||
|
||
function concurrentDefault(): number { | ||
return configurationValue<number>("pool.concurrent", 5); | ||
} | ||
|
||
/** | ||
* Execute all provided promises with a max concurrency | ||
* Results will be in the same order as the provided promises; if one promise rejects, | ||
* execution is stopped and the returned promise is rejected as well. | ||
* @param promises all promises to execute | ||
* @param concurrent the max number of concurrent promise executions | ||
*/ | ||
export async function executeAll<T>(promises: Array<() => Promise<T>>, | ||
concurrent: number = concurrentDefault()): Promise<T[]> { | ||
let index = 0; | ||
const results: any[] = []; | ||
const producer = () => { | ||
if (index < promises.length) { | ||
const promise = promises[index](); | ||
results[index] = promise; | ||
index++; | ||
return promise; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
const PromisePool = require("es6-promise-pool"); | ||
const pool = new PromisePool(producer, concurrent); | ||
|
||
pool.addEventListener("fulfilled", (event: any) => { | ||
results[results.indexOf(event.data.promise)] = event.data.result; | ||
}); | ||
|
||
await pool.start(); // start only returns a promise; not an [] of results | ||
|
||
return results; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright © 2018 Atomist, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import * as assert from "power-assert"; | ||
import { executeAll } from "../../lib/util/pool"; | ||
|
||
describe("pool", () => { | ||
|
||
describe("executeAll", () => { | ||
|
||
it("should preserve order", async () => { | ||
const results = await executeAll([() => { | ||
return new Promise<string>(resolve => { | ||
setTimeout(() => resolve("1"), 1000); | ||
}); | ||
}, () => { | ||
return new Promise<string>(resolve => { | ||
setTimeout(() => resolve("2"), 500); | ||
}); | ||
}, () => { | ||
return new Promise<string>(resolve => { | ||
setTimeout(() => resolve("3"), 100); | ||
}); | ||
}], 1); | ||
assert.deepStrictEqual(results, ["1", "2", "3"]); | ||
}); | ||
|
||
it("should reject with results and errors", async () => { | ||
try { | ||
await executeAll([() => { | ||
return new Promise<string>(resolve => { | ||
setTimeout(() => resolve("1"), 1000); | ||
}); | ||
}, () => { | ||
return new Promise<string>((resolve, reject) => { | ||
reject(new Error("2")); | ||
}); | ||
}, () => { | ||
return new Promise<string>(resolve => { | ||
setTimeout(() => resolve("3"), 100); | ||
}); | ||
}], 1); | ||
} catch (err) { | ||
assert.strictEqual(err.message, "2"); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
}); |