Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use os.availableConcurrency #556

Merged
merged 3 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,9 @@ This class extends [`EventEmitter`][] from Node.js.
function. The default is `'default'`, indicating the default export of the
worker module.
* `minThreads`: (`number`) Sets the minimum number of threads that are always
running for this thread pool. The default is based on the number of
available CPUs.
running for this thread pool. The default is the number provided by [`os.availableParallelism`](https://nodejs.org/api/os.html#osavailableparallelism).
* `maxThreads`: (`number`) Sets the maximum number of threads that are
running for this thread pool. The default is based on the number of
available CPUs.
running for this thread pool. The default is the number provided by [`os.availableParallelism`](https://nodejs.org/api/os.html#osavailableparallelism) * 1.5.
* `idleTimeout`: (`number`) A timeout in milliseconds that specifies how long
a `Worker` is allowed to be idle, i.e. not handling any tasks, before it is
shut down. By default, this is immediate. **Tip**: *The default `idleTimeout`
Expand Down
15 changes: 4 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Worker, MessageChannel, MessagePort, receiveMessageOnPort } from 'worker_threads';
import { once, EventEmitterAsyncResource } from 'events';
import { AsyncResource } from 'async_hooks';
import { cpus } from 'os';
import { availableParallelism } from 'os';
import { fileURLToPath, URL } from 'url';
import { resolve } from 'path';
import { inspect, types } from 'util';
Expand Down Expand Up @@ -30,14 +30,7 @@ import {
import { version } from '../package.json';
import { setTimeout as sleep } from 'timers/promises';

const cpuCount : number = (() => {
try {
return cpus().length;
} catch {
/* istanbul ignore next */
return 1;
}
})();
const cpuParallelism : number = availableParallelism();

/* eslint-disable camelcase */
interface HistogramSummary {
Expand Down Expand Up @@ -199,8 +192,8 @@ interface FilledOptions extends Options {
const kDefaultOptions : FilledOptions = {
filename: null,
name: 'default',
minThreads: Math.max(Math.floor(cpuCount / 2), 1),
maxThreads: cpuCount * 1.5,
minThreads: Math.max(Math.floor(cpuParallelism / 2), 1),
maxThreads: cpuParallelism * 1.5,
idleTimeout: 0,
maxQueue: Infinity,
concurrentTasksPerWorker: 1,
Expand Down