Skip to content

Instantly share code, notes, and snippets.

View batrudinych's full-sized avatar

batrudinych batrudinych

View GitHub Profile
@batrudinych
batrudinych / [s3-redeploy]-sequential-files-uploading.js
Last active January 9, 2019 09:48
[s3-redeploy]-sequential-files-uploading
const fs = require('fs');
const aws = require('aws-sdk');
const s3Client = new aws.S3();
async function uploadFilesToS3(fileNames) {
for (const fileName of fileNames) {
const fileStream = fs.createReadStream(fileName);
await s3Client.upload({
Key: fileName,
Body: fileStream,
@batrudinych
batrudinych / [s3-redeploy]-parallel-files-uploading.js
Last active January 9, 2019 10:26
[s3-redeploy]-parallel-files-uploading
// Limit of promises executed in parallel
// We don't want to upload more than 3 files simultaneously
const concurrency = 3;
// Returns a promise, which means uploading is fired once function is called
function uploadSingleFileToS3(fileName) {
const fileStream = fs.createReadStream(fileName);
return s3Client.upload({
Key: fileName,
Body: fileStream,
@batrudinych
batrudinych / [s3-redeploy]-parallel-hashes-computation.js
Created January 9, 2019 10:31
[s3-redeploy]-parallel-hashes-computation
const crypto = require('crypto');
const fs = require('fs');
// Limit of files to be processed simultaneously
const concurrency = 3;
// Promisified hash computation with streams
function computeSingleFileHash(fileName) {
const hash = crypto.createHash('md5');
const fileStream = fs.createReadStream(fileName);
@batrudinych
batrudinych / parallel-promises.js
Created January 14, 2019 10:19
Run promises in parallel, applying the concurrency limit
**
* Run Promises in parallel with a concurrency limit
* @param args - List of arguments to be passed to 'fn'
* @param fn - A function to apply to each argument. MUST return a Promise
* @param concurrency - Allowed amount of Promises to be run in parallel
* @returns {Promise} - A Promise, which eventually resolved with a list of results.
* Order corresponds to the list of arguments.
*/
function mapPromises(args, fn, concurrency = 1) {
if (!args.length) return Promise.resolve([]);
@batrudinych
batrudinych / [parallel-promises]-common.js
Last active February 4, 2019 13:29
List of variables and function shared between invocations
// Used to watch amount of Promises executing in a single moment of time
let counter = 0;
let interval;
// Overall amount of operations
const numberOfOperations = 25;
// Arguments per operation
const listOfArguments = [];
// Delays per operation to fake async request
const listOfDelays = [];
@batrudinych
batrudinych / [parallel-promises]-take0.js
Created January 14, 2019 10:58
Running async operations sequentially and harvesting the results
async function take0() {
// Harvesting
const results = [];
for (const argument of listOfArguments) {
const index = await asyncOperation(argument);
results.push(index);
}
return results;
}
@batrudinych
batrudinych / [parallel-promises]-take1.js
Created January 14, 2019 10:58
Running async operations in parallel and harvesting the results
async function take1() {
// Running Promises in parallel
const listOfPromises = listOfArguments.map(asyncOperation);
// Harvesting
const results = [];
for (const promise of listOfPromises) {
const index = await promise;
results.push(index);
}
return results;
@batrudinych
batrudinych / [parallel-promises]-take2.js
Created January 14, 2019 10:58
Running async operations in parallel and harvesting the results
async function take2() {
// Running Promises in parallel
const listOfPromises = listOfArguments.map(asyncOperation);
// Harvesting
return await Promise.all(listOfPromises);
}
@batrudinych
batrudinych / [parallel-promises]-take3-subtake0.js
Created January 14, 2019 10:58
Running async operations in parallel and harvesting the results. Concurrency limit applied incorrectly
async function take3subtake0() {
const concurrencyLimit = 5;
let results = [];
const batchesCount = Math.ceil(numberOfOperations / concurrencyLimit);
// Running Promises in parallel in batches
for (let i = 0; i < batchesCount; i++) {
const batchStart = i * concurrencyLimit;
const batchArguments = listOfArguments.slice(batchStart, batchStart + concurrencyLimit);
const batchPromises = batchArguments.map(asyncOperation);
@batrudinych
batrudinych / [parallel-promises]-take3-subtake1-part0.js
Created January 14, 2019 10:58
Running async operations in parallel. Concurrency limit applied correctly
async function take3subtake1part0() {
const concurrencyLimit = 5;
const argsCopy = listOfArguments.slice();
const promises = new Array(concurrencyLimit).fill(Promise.resolve());
// Recursively chain the next Promise to the currently executed Promise
function chainNext(p) {
if (argsCopy.length) {
const arg = argsCopy.shift();
return p.then(() => {
const operationPromise = asyncOperation(arg);