import { dirname, extname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { FixedClusterPool, availableParallelism } from 'poolifier'
-import { type ClusterWorkerData, type ClusterWorkerResponse } from './types.js'
+import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js'
const webSocketServerWorkerFile = join(
dirname(fileURLToPath(import.meta.url)),
`websocket-server-worker${extname(fileURLToPath(import.meta.url))}`
)
+const requestHandlerWorkerFile = join(
+ dirname(fileURLToPath(import.meta.url)),
+ `request-handler-worker${extname(fileURLToPath(import.meta.url))}`
+)
+
const pool = new FixedClusterPool<ClusterWorkerData, ClusterWorkerResponse>(
Math.round(availableParallelism() / 2),
webSocketServerWorkerFile,
{
+ enableEvents: false,
onlineHandler: () => {
pool
- .execute({ port: 8080 })
+ .execute({
+ port: 8080,
+ maxWorkers:
+ Math.round(availableParallelism() / 4) < 1
+ ? 1
+ : Math.round(availableParallelism() / 4),
+ workerFile: requestHandlerWorkerFile,
+ enableTasksQueue: true,
+ tasksQueueOptions: {
+ concurrency: 8
+ },
+ errorHandler: (e: Error) => {
+ console.error('Thread worker error:', e)
+ }
+ })
.then(response => {
if (response.status) {
console.info(
`WebSocket server is listening in cluster worker on port ${response.port}`
)
}
- return null
+ return undefined
})
.catch(error => {
console.error(