@setu-ts/worker-pool-plugin — run CPU work on threads
Add it: setu add worker-pool adds it and inserts WorkerPoolPlugin() into a recognized
functional setu.config.ts. It provides CAPABILITIES.WORKER_POOL (IWorkerPool).
Submit an existing task from a route
The application-owned task module must register its worker handler through the worker-side helper
from @setu-ts/runtime/worker before it can receive work.
import { CAPABILITIES } from '@setu-ts/common';
import type { IRouterApi, IWorkerPool } from '@setu-ts/common';
export function registerThumbnailRoutes(router: IRouterApi): void {
router.post('/thumbnail', {
handler: async (ctx) => {
const pool = ctx.services.get<IWorkerPool>(CAPABILITIES.WORKER_POOL);
const image = await pool.run<Uint8Array, Uint8Array>(
new URL('./tasks/resize-image.ts', import.meta.url).href,
await ctx.request.bytes(),
{ timeoutMs: 15_000 },
);
return ctx.response.header('content-type', 'image/png').send(image);
},
});
}
Inspect pool load
import { CAPABILITIES } from '@setu-ts/common';
import type { IWorkerPool } from '@setu-ts/common';
const pools = ctx.services.get<IWorkerPool>(CAPABILITIES.WORKER_POOL).stats();
ctx.response.json(pools);
Behaviour the types do not show
- A pool is created on first use of each task-module specifier.
- Payloads cross by structured clone; functions cannot be sent. Shared memory remains shared.
taskTimeoutMsdefaults to 30000 milliseconds;0disables it. Per-calltimeoutMsoverrides it.maxQueuedefaults to 1024;startupTimeoutMsdefaults to 10000 milliseconds and cannot be disabled.- Without runtime worker support, registration succeeds but
runrejects withWorkerPoolUnavailableError; the health indicator reports up withavailable: false. - Application shutdown terminates workers and rejects pending tasks.
Other options
maxWorkerscaps all pools together;Infinitydisables that cap.defaultPoolSizesets per-module sizing;poolsoverridessize,maxQueueandtaskTimeoutMsby specifier.hostinjects a worker host;WorkerQueueFullErrorandWorkerTaskTimeoutErroridentify overload and deadlines.