Skip to content

Commit

Permalink
fix: support nodejs v16.x again (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
xqin authored May 22, 2024
1 parent 5b46247 commit d50391f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [18.x, 20.x, 22.x]
node-version: [16.x, 18.x, 20.x, 22.x]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
Expand All @@ -53,7 +53,7 @@ jobs:
key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ runner.os }}-node-${{ matrix.node-version }}-
- name: Install Dependencies
run: npm install
- name: Run Tests
Expand Down
14 changes: 14 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Histogram } from 'node:perf_hooks';
import { fileURLToPath, URL } from 'node:url';
import { availableParallelism, cpus } from 'node:os';

import type { HistogramSummary } from './types';
import { kMovable, kTransferable, kValue } from './symbols';
Expand Down Expand Up @@ -87,3 +88,16 @@ export function maybeFileURLToPath (filename : string) : string {
? fileURLToPath(new URL(filename))
: filename;
}

// TODO: drop on v5
export function getAvailableParallelism () : number {
if (typeof availableParallelism === 'function') {
return availableParallelism();
}

try {
return cpus().length;
} catch {
return 1;
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Worker, MessageChannel, MessagePort, receiveMessageOnPort } from 'node:worker_threads';
import { once, EventEmitterAsyncResource } from 'node:events';
import { AsyncResource } from 'node:async_hooks';
import { availableParallelism } from 'node:os';
import { resolve } from 'node:path';
import { inspect, types } from 'node:util';
import { RecordableHistogram, createHistogram, performance } from 'node:perf_hooks';
Expand Down Expand Up @@ -49,6 +48,7 @@ import {
isMovable,
createHistogramSummary,
toHistogramIntegerNano,
getAvailableParallelism,
maybeFileURLToPath
} from './common';
import FixedQueue from './fixed-queue';
Expand All @@ -61,7 +61,7 @@ const { version } = JSON.parse(
}
));

const cpuParallelism : number = availableParallelism();
const cpuParallelism : number = getAvailableParallelism();

interface Options {
filename? : string | null,
Expand Down
6 changes: 3 additions & 3 deletions test/post-task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MessageChannel } from 'worker_threads';
import { availableParallelism } from 'os';
import { getAvailableParallelism } from '../dist/common';
import Piscina from '..';
import { test } from 'tap';
import { resolve } from 'path';
Expand Down Expand Up @@ -168,7 +168,7 @@ test('Piscina.maxThreads should return the max number of threads to be used (def
filename: resolve(__dirname, 'fixtures/eval.js')
});

const maxThreads = (availableParallelism() ?? 1) * 1.5;
const maxThreads = getAvailableParallelism() * 1.5;

equal(pool.maxThreads, maxThreads);
});
Expand All @@ -189,7 +189,7 @@ test('Piscina.minThreads should return the max number of threads to be used (def
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js')
});
const minThreads = Math.max(Math.floor(availableParallelism() / 2), 1);
const minThreads = Math.max(Math.floor(getAvailableParallelism() / 2), 1);

plan(1);
equal(pool.minThreads, minThreads);
Expand Down

0 comments on commit d50391f

Please sign in to comment.