How to use the async-sema.Sema function in async-sema

To help you get started, we’ve selected a few async-sema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zeit / next.js / run-tests.js View on Github external
]

  if (groupArg) {
    const groupParts = groupArg.split('/')
    const groupPos = parseInt(groupParts[0], 10)
    const groupTotal = parseInt(groupParts[1], 10)
    const numPerGroup = Math.ceil(testNames.length / groupTotal)
    let offset = groupPos === 1 ? 0 : (groupPos - 1) * numPerGroup - 1
    // if there's an odd number of suites give the first group the extra
    if (testNames.length % 2 !== 0 && groupPos !== 1) offset++
    testNames = testNames.splice(offset, numPerGroup)
  }

  console.log('Running tests:', '\n', ...testNames.map(name => `${name}\n`))

  const sema = new Sema(concurrency, { capacity: testNames.length })
  const jestPath = path.join(
    path.dirname(require.resolve('jest-cli/package')),
    'bin/jest.js'
  )
  const children = new Set()

  const runTest = (test = '') =>
    new Promise((resolve, reject) => {
      const start = new Date().getTime()
      const child = spawn(
        'node',
        [jestPath, '--runInBand', '--forceExit', '--verbose', test],
        {
          stdio: 'inherit',
        }
      )
github zeit / now / packages / now-next / src / utils.ts View on Github external
return pseudoLayer;
}

interface CreateLambdaFromPseudoLayersOptions {
  files: Files;
  layers: PseudoLayer[];
  handler: string;
  runtime: string;
  memory?: number;
  maxDuration?: number;
  environment?: { [name: string]: string };
}

// measured with 1, 2, 5, 10, and `os.cpus().length || 5`
// and sema(1) produced the best results
const createLambdaSema = new Sema(1);

export async function createLambdaFromPseudoLayers({
  files,
  layers,
  handler,
  runtime,
  memory,
  maxDuration,
  environment = {},
}: CreateLambdaFromPseudoLayersOptions) {
  await createLambdaSema.acquire();
  const zipFile = new ZipFile();
  const addedFiles = new Set();

  const names = Object.keys(files).sort();
  const symlinkTargets = new Map();
github zeit / next.js / packages / next / lib / recursive-copy.ts View on Github external
dest: string,
  {
    concurrency = 32,
    overwrite = false,
    filter = () => true,
  }: {
    concurrency?: number
    overwrite?: boolean
    filter?(path: string): boolean
  } = {}
) {
  const cwdPath = process.cwd()
  const from = path.resolve(cwdPath, source)
  const to = path.resolve(cwdPath, dest)

  const sema = new Sema(concurrency)

  async function _copy(item: string) {
    const target = item.replace(from, to)
    const stats = await stat(item)

    await sema.acquire()

    if (stats.isDirectory()) {
      try {
        await mkdir(target)
      } catch (err) {
        // do not throw `folder already exists` errors
        if (err.code !== 'EEXIST') {
          throw err
        }
      }
github zeit / now / packages / now-client / src / utils / index.ts View on Github external
import { DeploymentFile } from './hashes';
import { parse as parseUrl } from 'url';
import fetch_, { RequestInit } from 'node-fetch';
import { join, sep } from 'path';
import qs from 'querystring';
import ignore from 'ignore';
import { pkgVersion } from '../pkg';
import { NowClientOptions, DeploymentOptions, NowConfig } from '../types';
import { Sema } from 'async-sema';
import { readFile } from 'fs-extra';
const semaphore = new Sema(10);

export const API_FILES = '/v2/now/files';
export const API_DELETE_DEPLOYMENTS_LEGACY = '/v2/now/deployments';

export const EVENTS = new Set([
  // File events
  'hashes-calculated',
  'file_count',
  'file-uploaded',
  'all-files-uploaded',
  // Deployment events
  'created',
  'ready',
  'alias-assigned',
  'warning',
  'error',
github zeit / async-sema / examples / pooling.js View on Github external
async function f() {
	const red = new Sema(3, {
		initFn: () => redis().createClient(process.env.REDIS_URL)
	});

	const db = await red.acquire();
	console.log(await db.get('id'));
	red.release(db);

	const dbs = await red.drain();
	dbs.map(db => db.quit());
}
f()
github zeit / async-sema / examples / basic.js View on Github external
async function f() {
	const arr = [];

	for (let i = 0; i < 100; i++) arr.push(i + 1);

	const s = new Sema(13, { capacity: arr.length });
	await Promise.all(
		arr.map(async elem => {
			await s.acquire();
			console.log(elem, s.nrWaiting());
			await new Promise(resolve =>
				setTimeout(resolve, getRnd(500, 3000))
			);
			s.release();
		})
	);
	console.log('hello');
}
f()
github zeit / now / packages / now-client / src / utils / hashes.ts View on Github external
async function hashes(files: string[]): Promise> {
  const map = new Map();
  const semaphore = new Sema(100);

  await Promise.all(
    files.map(
      async (name: string): Promise => {
        await semaphore.acquire();
        const data = await fs.readFile(name);
        const { mode } = await fs.stat(name);

        const h = hash(data);
        const entry = map.get(h);

        if (entry) {
          entry.names.push(name);
        } else {
          map.set(h, { names: [name], data, mode });
        }
github zeit / next.js / packages / next / build / flying-shuttle.ts View on Github external
return hash !== hashes[file]
    })
    .some(Boolean)
}

export class FlyingShuttle {
  private apexShuttleDirectory: string
  private flyingShuttleId: string

  private buildId: string
  private pagesDirectory: string
  private distDirectory: string
  private parentCacheIdentifier: string

  private _shuttleBuildId: string | undefined
  private _restoreSema = new Sema(1)
  private _recalledManifest: ChunkGraphManifest = {
    sharedFiles: [],
    pages: {},
    pageChunks: {},
    chunks: {},
    hashes: {},
  }

  constructor({
    buildId,
    pagesDirectory,
    distDirectory,
    cacheIdentifier,
  }: {
    buildId: string
    pagesDirectory: string

async-sema

Semaphore using `async` and `await`

MIT
Latest version published 3 years ago

Package Health Score

74 / 100
Full package analysis