Skip to content

Configuring Worker Threads

For enable multi-threading you can use fileSystemRouter({ workers: true }) which creates a thread pool up to the number of available logical processors or use specify the number of workers using fileSystemRouter({ workerCount: 4 }).

src/main.ts
import { fileSystemRouter } from "keiro/node";
import http from "node:http";
http
.createServer(
fileSystemRouter({
workers: { workerCount: 4 },
}),
)
.listen(3000);

Worker pool

The workers are spawn inside a WorkerPool, you can specify the worker using the pool option:

const enum WorkerPoolType {
Dynamic = 1,
Fixed = 2,
}

By default it uses the WorkerPoolType.Fixed pool, you can also pass function that returns a WorkerPool:

(workerCount: number, filename: string, options?: WorkerOptions) => WorkerPool

How it works?

Internally we use node Worker API, and create a pool of workers. When a request comes we take an available worker and serialize the request and send it to the worker and then deserialize the response of the worker and send it as a response.

Each worker have its own router which is used to handle the request.

Is not multi-threading always faster?

No.

We recommend profiling your app before enabling workers, unless you are handling a big number of request you may not see any difference when enabling workers, it may be even slighly inneficient to using workers for a few number of request per second.

Checkout the Worker threads example.