Skip to content

Commit

Permalink
chore: use @napi-rs/nice to support Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Sep 15, 2024
1 parent 9b6633b commit fdba994
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 64 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ This class extends [`EventEmitter`][] from Node.js.
alternative implementation. See [Custom Task Queues][] for additional detail.
* `niceIncrement`: (`number`) An optional value that decreases priority for
the individual threads, i.e. the higher the value, the lower the priority
of the Worker threads. This value is only used on Linux and requires the
optional [`nice-napi`][] module to be installed.
See [`nice(2)`][] for more details.
of the Worker threads. This value is used on Unix/Windows and requires the
optional [`@napi-rs/nice`][https://www.npmjs.com/package/@napi-rs/nice] module to be installed.
See [`nice(2)`][https://linux.die.net/man/2/nice] for more details.
* `trackUnmanagedFds`: (`boolean`) An optional setting that, when `true`, will
cause Workers to track file descriptors managed using `fs.open()` and
`fs.close()`, and will close them automatically when the Worker exits.
Expand Down Expand Up @@ -842,8 +842,8 @@ are no one set of options that are going to work best.
On Linux systems that support [`nice(2)`][], Piscina is capable of setting
the priority of every worker in the pool. To use this mechanism, an additional
optional native addon dependency (`nice-napi`, `npm i nice-napi`) is required.
Once [`nice-napi`][] is installed, creating a `Piscina` instance with the
optional native addon dependency (`@napi-rs/nice`, `npm i @napi-rs/nice`) is required.
Once [`@napi-rs/nice`][] is installed, creating a `Piscina` instance with the
`niceIncrement` configuration option will set the priority for the pool:
```js
Expand Down Expand Up @@ -988,7 +988,7 @@ Piscina development is sponsored by [NearForm Research][].
[`postMessage`]: https://nodejs.org/api/worker_threads.html#worker_threads_port_postmessage_value_transferlist
[`examples/task-queue`]: https://github.com/jasnell/piscina/blob/master/examples/task-queue/index.js
[`nice(2)`]: https://linux.die.net/man/2/nice
[`nice-napi`]: https://npmjs.org/package/nice-napi
[`@napi-rs/nice`]: https://npmjs.org/package/@napi-rs/nice
[`runTime`]: #property-runtime-readonly
[Custom Task Queues]: #custom_task_queues
[ES modules]: https://nodejs.org/api/esm.html
Expand Down
17 changes: 10 additions & 7 deletions docs/docs/advanced-topics/performance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,21 @@ are no one set of options that are going to work best.

### Thread priority on Linux systems

On Linux systems that support [`nice(2)`][], Piscina is capable of setting
On Unix systems that support [`nice(2)`][https://linux.die.net/man/2/nice] and Windows, Piscina is capable of setting
the priority of every worker in the pool. To use this mechanism, an additional
optional native addon dependency (`nice-napi`, `npm i nice-napi`) is required.
Once [`nice-napi`][] is installed, creating a `Piscina` instance with the
`niceIncrement` configuration option will set the priority for the pool:
optional native addon dependency (`@napi-rs/nice`, `npm i @napi-rs/nice`) is required.
Once [`@napi-rs/nice`][https://www.npmjs.com/package/@napi-rs/nice] is installed, creating a `Piscina`instance with the`niceIncrement` configuration option will set the priority for the pool:

```js
const Piscina = require('piscina');
const Piscina = require('piscina')
const { WindowsThreadPriority } = require('@napi-rs/nice')
const pool = new Piscina({
worker: '/absolute/path/to/worker.js',
niceIncrement: 20
});
niceIncrement:
process.platform !== 'win32'
? 20
: WindowsThreadPriority.ThreadPriorityHighest,
})
```

The higher the `niceIncrement`, the lower the CPU scheduling priority will be
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/api-reference/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ This class extends [`EventEmitter`](https://nodejs.org/api/events.html) from Nod
alternative implementation. See [Custom Task Queues](https://github.com/piscinajs/piscina#custom_task_queues) for additional detail.
- `niceIncrement`: (`number`) An optional value that decreases priority for
the individual threads, i.e. the higher the value, the lower the priority
of the Worker threads. This value is only used on Linux and requires the
optional [`nice-napi`](https://npmjs.org/package/nice-napi) module to be installed.
See [`nice(2)`](https://linux.die.net/man/2/nice) for more details.
of the Worker threads. This value is used on Unix/Windows and requires the
optional [`@napi-rs/nice`](https://npmjs.org/package/@napi-rs/nice) module to be installed.
See [`nice(2)`](https://linux.die.net/man/2/nice) and [`SetThreadPriority`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadpriority) for more details.
- `trackUnmanagedFds`: (`boolean`) An optional setting that, when `true`, will
cause Workers to track file descriptors managed using `fs.open()` and
`fs.close()`, and will close them automatically when the Worker exits.
Expand Down
270 changes: 238 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"typescript": "5.5.4"
},
"optionalDependencies": {
"nice-napi": "^1.0.2"
"@napi-rs/nice": "^1.0.0"
},
"eslintConfig": {
"rules": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,8 @@ export default class Piscina<T = any, R = any> extends EventEmitterAsyncResource
throw new TypeError('options.taskQueue must be a TaskQueue object');
}
if (options.niceIncrement !== undefined &&
(typeof options.niceIncrement !== 'number' || options.niceIncrement < 0)) {
throw new TypeError('options.niceIncrement must be a non-negative integer');
(typeof options.niceIncrement !== 'number' || (options.niceIncrement < 0 && process.platform !== 'win32'))) {
throw new TypeError('options.niceIncrement must be a non-negative integer on Unix systems');
}
if (options.trackUnmanagedFds !== undefined &&
typeof options.trackUnmanagedFds !== 'boolean') {
Expand Down
6 changes: 2 additions & 4 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ parentPort!.on('message', (message: StartupMessage) => {
const { port, sharedBuffer, filename, name, niceIncrement } = message;
(async function () {
try {
if (niceIncrement !== 0 && process.platform === 'linux') {
// ts-ignore because the dependency is not installed on Windows.
// @ts-ignore
(await import('nice-napi')).default(niceIncrement);
if (niceIncrement !== 0) {
(await import('@napi-rs/nice')).nice(niceIncrement);
}
} catch {}

Expand Down
Loading

0 comments on commit fdba994

Please sign in to comment.