Skip to content

Commit fdba994

Browse files
committed
chore: use @napi-rs/nice to support Windows
1 parent 9b6633b commit fdba994

File tree

8 files changed

+268
-64
lines changed

8 files changed

+268
-64
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ This class extends [`EventEmitter`][] from Node.js.
339339
alternative implementation. See [Custom Task Queues][] for additional detail.
340340
* `niceIncrement`: (`number`) An optional value that decreases priority for
341341
the individual threads, i.e. the higher the value, the lower the priority
342-
of the Worker threads. This value is only used on Linux and requires the
343-
optional [`nice-napi`][] module to be installed.
344-
See [`nice(2)`][] for more details.
342+
of the Worker threads. This value is used on Unix/Windows and requires the
343+
optional [`@napi-rs/nice`][https://www.npmjs.com/package/@napi-rs/nice] module to be installed.
344+
See [`nice(2)`][https://linux.die.net/man/2/nice] for more details.
345345
* `trackUnmanagedFds`: (`boolean`) An optional setting that, when `true`, will
346346
cause Workers to track file descriptors managed using `fs.open()` and
347347
`fs.close()`, and will close them automatically when the Worker exits.
@@ -842,8 +842,8 @@ are no one set of options that are going to work best.
842842
843843
On Linux systems that support [`nice(2)`][], Piscina is capable of setting
844844
the priority of every worker in the pool. To use this mechanism, an additional
845-
optional native addon dependency (`nice-napi`, `npm i nice-napi`) is required.
846-
Once [`nice-napi`][] is installed, creating a `Piscina` instance with the
845+
optional native addon dependency (`@napi-rs/nice`, `npm i @napi-rs/nice`) is required.
846+
Once [`@napi-rs/nice`][] is installed, creating a `Piscina` instance with the
847847
`niceIncrement` configuration option will set the priority for the pool:
848848
849849
```js
@@ -988,7 +988,7 @@ Piscina development is sponsored by [NearForm Research][].
988988
[`postMessage`]: https://nodejs.org/api/worker_threads.html#worker_threads_port_postmessage_value_transferlist
989989
[`examples/task-queue`]: https://github.com/jasnell/piscina/blob/master/examples/task-queue/index.js
990990
[`nice(2)`]: https://linux.die.net/man/2/nice
991-
[`nice-napi`]: https://npmjs.org/package/nice-napi
991+
[`@napi-rs/nice`]: https://npmjs.org/package/@napi-rs/nice
992992
[`runTime`]: #property-runtime-readonly
993993
[Custom Task Queues]: #custom_task_queues
994994
[ES modules]: https://nodejs.org/api/esm.html

docs/docs/advanced-topics/performance.mdx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,21 @@ are no one set of options that are going to work best.
8383

8484
### Thread priority on Linux systems
8585

86-
On Linux systems that support [`nice(2)`][], Piscina is capable of setting
86+
On Unix systems that support [`nice(2)`][https://linux.die.net/man/2/nice] and Windows, Piscina is capable of setting
8787
the priority of every worker in the pool. To use this mechanism, an additional
88-
optional native addon dependency (`nice-napi`, `npm i nice-napi`) is required.
89-
Once [`nice-napi`][] is installed, creating a `Piscina` instance with the
90-
`niceIncrement` configuration option will set the priority for the pool:
88+
optional native addon dependency (`@napi-rs/nice`, `npm i @napi-rs/nice`) is required.
89+
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:
9190

9291
```js
93-
const Piscina = require('piscina');
92+
const Piscina = require('piscina')
93+
const { WindowsThreadPriority } = require('@napi-rs/nice')
9494
const pool = new Piscina({
9595
worker: '/absolute/path/to/worker.js',
96-
niceIncrement: 20
97-
});
96+
niceIncrement:
97+
process.platform !== 'win32'
98+
? 20
99+
: WindowsThreadPriority.ThreadPriorityHighest,
100+
})
98101
```
99102

100103
The higher the `niceIncrement`, the lower the CPU scheduling priority will be

docs/docs/api-reference/class.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ This class extends [`EventEmitter`](https://nodejs.org/api/events.html) from Nod
7979
alternative implementation. See [Custom Task Queues](https://github.com/piscinajs/piscina#custom_task_queues) for additional detail.
8080
- `niceIncrement`: (`number`) An optional value that decreases priority for
8181
the individual threads, i.e. the higher the value, the lower the priority
82-
of the Worker threads. This value is only used on Linux and requires the
83-
optional [`nice-napi`](https://npmjs.org/package/nice-napi) module to be installed.
84-
See [`nice(2)`](https://linux.die.net/man/2/nice) for more details.
82+
of the Worker threads. This value is used on Unix/Windows and requires the
83+
optional [`@napi-rs/nice`](https://npmjs.org/package/@napi-rs/nice) module to be installed.
84+
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.
8585
- `trackUnmanagedFds`: (`boolean`) An optional setting that, when `true`, will
8686
cause Workers to track file descriptors managed using `fs.open()` and
8787
`fs.close()`, and will close them automatically when the Worker exits.

package-lock.json

Lines changed: 238 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"typescript": "5.5.4"
5757
},
5858
"optionalDependencies": {
59-
"nice-napi": "^1.0.2"
59+
"@napi-rs/nice": "^1.0.0"
6060
},
6161
"eslintConfig": {
6262
"rules": {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,8 @@ export default class Piscina<T = any, R = any> extends EventEmitterAsyncResource
708708
throw new TypeError('options.taskQueue must be a TaskQueue object');
709709
}
710710
if (options.niceIncrement !== undefined &&
711-
(typeof options.niceIncrement !== 'number' || options.niceIncrement < 0)) {
712-
throw new TypeError('options.niceIncrement must be a non-negative integer');
711+
(typeof options.niceIncrement !== 'number' || (options.niceIncrement < 0 && process.platform !== 'win32'))) {
712+
throw new TypeError('options.niceIncrement must be a non-negative integer on Unix systems');
713713
}
714714
if (options.trackUnmanagedFds !== undefined &&
715715
typeof options.trackUnmanagedFds !== 'boolean') {

src/worker.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ parentPort!.on('message', (message: StartupMessage) => {
8484
const { port, sharedBuffer, filename, name, niceIncrement } = message;
8585
(async function () {
8686
try {
87-
if (niceIncrement !== 0 && process.platform === 'linux') {
88-
// ts-ignore because the dependency is not installed on Windows.
89-
// @ts-ignore
90-
(await import('nice-napi')).default(niceIncrement);
87+
if (niceIncrement !== 0) {
88+
(await import('@napi-rs/nice')).nice(niceIncrement);
9189
}
9290
} catch {}
9391

0 commit comments

Comments
 (0)