-import type { Server } from 'http'
-import type { AddressInfo } from 'net'
+import type { Server } from 'node:http'
+import type { AddressInfo } from 'node:net'
+
+import express, { type Express, type Request, type Response } from 'express'
import {
+ availableParallelism,
ClusterWorker,
DynamicThreadPool,
- availableParallelism
} from 'poolifier'
-import express, { type Express, type Request, type Response } from 'express'
-import {
- type ClusterWorkerData,
- type ClusterWorkerResponse,
- type DataPayload,
- type ThreadWorkerData,
- type ThreadWorkerResponse
+
+import type {
+ ClusterWorkerData,
+ ClusterWorkerResponse,
+ DataPayload,
+ ThreadWorkerData,
+ ThreadWorkerResponse,
} from './types.js'
const emptyFunction = (): void => {
- /** Intentional */
+ /* Intentional */
}
class ExpressWorker extends ClusterWorker<
-ClusterWorkerData,
-ClusterWorkerResponse
+ ClusterWorkerData,
+ ClusterWorkerResponse
> {
private static server: Server
private static requestHandlerPool: DynamicThreadPool<
- ThreadWorkerData<DataPayload>,
- ThreadWorkerResponse<DataPayload>
+ ThreadWorkerData<DataPayload>,
+ ThreadWorkerResponse<DataPayload>
>
private static readonly startExpress = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
const { port, workerFile, minWorkers, maxWorkers, ...poolOptions } =
- workerData as ClusterWorkerData
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ workerData!
ExpressWorker.requestHandlerPool = new DynamicThreadPool<
- ThreadWorkerData<DataPayload>,
- ThreadWorkerResponse<DataPayload>
+ ThreadWorkerData<DataPayload>,
+ ThreadWorkerResponse<DataPayload>
>(
minWorkers ?? 1,
maxWorkers ?? availableParallelism(),
application.all('/api/echo', (req: Request, res: Response) => {
ExpressWorker.requestHandlerPool
.execute({ data: req.body }, 'echo')
- .then((response) => {
+ .then(response => {
return res.send(response.data).end()
})
.catch(emptyFunction)
application.get('/api/factorial/:number', (req: Request, res: Response) => {
const { number } = req.params
ExpressWorker.requestHandlerPool
- .execute({ data: { number: parseInt(number) } }, 'factorial')
- .then((response) => {
+ .execute({ data: { number: Number.parseInt(number) } }, 'factorial')
+ .then(response => {
return res.send(response.data).end()
})
.catch(emptyFunction)
})
+ let listenerPort: number | undefined
ExpressWorker.server = application.listen(port, () => {
+ listenerPort = (ExpressWorker.server.address() as AddressInfo).port
console.info(
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
- `⚡️[express server]: Express server is started in cluster worker at http://localhost:${port}/`
+ `⚡️[express server]: Express server is started in cluster worker at http://localhost:${listenerPort.toString()}/`
)
})
return {
status: true,
- port: (ExpressWorker.server.address() as AddressInfo)?.port ?? port
+ port: listenerPort ?? port,
}
}
killHandler: async () => {
await ExpressWorker.requestHandlerPool.destroy()
ExpressWorker.server.close()
- }
+ },
})
}
}