import type { AddressInfo } from 'node:net'
import { ClusterWorker } from 'poolifier'
-import Fastify from 'fastify'
+import Fastify, { type FastifyInstance } from 'fastify'
import type { WorkerData, WorkerResponse } from './types.js'
const factorial: (n: number) => number = n => {
return factorial(n - 1) * n
}
-const startFastify = async (
- workerData?: WorkerData
-): Promise<WorkerResponse> => {
- const { port } = workerData as WorkerData
- const fastify = Fastify({
- logger: true
- })
-
- fastify.all('/api/echo', request => {
- return request.body
- })
-
- fastify.get<{
- Params: { number: number }
- }>('/api/factorial/:number', request => {
- const { number } = request.params
- return { number: factorial(number) }
- })
-
- await fastify.listen({ port })
- return {
- status: true,
- port: (fastify.server.address() as AddressInfo).port
+class FastifyWorker extends ClusterWorker<WorkerData, WorkerResponse> {
+ private static fastify: FastifyInstance
+
+ private static readonly startFastify = async (
+ workerData?: WorkerData
+ ): Promise<WorkerResponse> => {
+ const { port } = workerData as WorkerData
+ FastifyWorker.fastify = Fastify({
+ logger: true
+ })
+
+ FastifyWorker.fastify.all('/api/echo', request => {
+ return request.body
+ })
+
+ FastifyWorker.fastify.get<{
+ Params: { number: number }
+ }>('/api/factorial/:number', request => {
+ const { number } = request.params
+ return { number: factorial(number) }
+ })
+
+ await FastifyWorker.fastify.listen({ port })
+ return {
+ status: true,
+ port: (FastifyWorker.fastify.server.address() as AddressInfo).port
+ }
}
-}
-class FastifyWorker extends ClusterWorker<WorkerData, WorkerResponse> {
public constructor () {
- super(startFastify)
+ super(FastifyWorker.startFastify)
}
}
import type { AddressInfo } from 'node:net'
import { ClusterWorker } from 'poolifier'
-import Fastify from 'fastify'
+import Fastify, { type FastifyInstance } from 'fastify'
import type { ClusterWorkerData, ClusterWorkerResponse } from './types.js'
import { fastifyPoolifier } from './fastify-poolifier.js'
-const startFastify = async (
- workerData?: ClusterWorkerData
-): Promise<ClusterWorkerResponse> => {
- const { port } = workerData as ClusterWorkerData
- const fastify = Fastify({
- logger: true
- })
-
- await fastify.register(fastifyPoolifier, workerData)
-
- fastify.all('/api/echo', async request => {
- return (await fastify.execute({ body: request.body }, 'echo')).body
- })
-
- fastify.get<{
- Params: { number: number }
- }>('/api/factorial/:number', async request => {
- const { number } = request.params
- return (await fastify.execute({ body: { number } }, 'factorial')).body
- })
-
- await fastify.listen({ port })
- return {
- status: true,
- port: (fastify.server.address() as AddressInfo).port
- }
-}
-
class FastifyWorker extends ClusterWorker<
ClusterWorkerData,
ClusterWorkerResponse
> {
+ private static fastify: FastifyInstance
+
+ private static readonly startFastify = async (
+ workerData?: ClusterWorkerData
+ ): Promise<ClusterWorkerResponse> => {
+ const { port } = workerData as ClusterWorkerData
+ FastifyWorker.fastify = Fastify({
+ logger: true
+ })
+
+ await FastifyWorker.fastify.register(fastifyPoolifier, workerData)
+
+ FastifyWorker.fastify.all('/api/echo', async request => {
+ return (
+ await FastifyWorker.fastify.execute({ body: request.body }, 'echo')
+ ).body
+ })
+
+ FastifyWorker.fastify.get<{
+ Params: { number: number }
+ }>('/api/factorial/:number', async request => {
+ const { number } = request.params
+ return (
+ await FastifyWorker.fastify.execute({ body: { number } }, 'factorial')
+ ).body
+ })
+
+ await FastifyWorker.fastify.listen({ port })
+ return {
+ status: true,
+ port: (FastifyWorker.fastify.server.address() as AddressInfo).port
+ }
+ }
+
public constructor () {
- super(startFastify)
+ super(FastifyWorker.startFastify)
}
}
return factorial(n - 1) * n
}
-const startWebSocketServer = (workerData?: WorkerData): WorkerResponse => {
- const { port } = workerData as WorkerData
- const wss = new WebSocketServer({ port }, () => {
- console.info(
- `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/`
- )
- })
+class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
+ private static wss: WebSocketServer
+
+ private static readonly startWebSocketServer = (
+ workerData?: WorkerData
+ ): WorkerResponse => {
+ const { port } = workerData as WorkerData
+ WebSocketServerWorker.wss = new WebSocketServer({ port }, () => {
+ console.info(
+ `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/`
+ )
+ })
- wss.on('connection', ws => {
- ws.on('error', console.error)
- ws.on('message', (message: RawData) => {
- const { type, data } = JSON.parse(
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
- message.toString()
- ) as MessagePayload<DataPayload>
- switch (type) {
- case MessageType.echo:
- ws.send(
- JSON.stringify({
- type: MessageType.echo,
- data
- })
- )
- break
- case MessageType.factorial:
- ws.send(
- JSON.stringify({
- type: MessageType.factorial,
- data: { number: factorial(data.number as number) }
- })
- )
- break
- }
+ WebSocketServerWorker.wss.on('connection', ws => {
+ ws.on('error', console.error)
+ ws.on('message', (message: RawData) => {
+ const { type, data } = JSON.parse(
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
+ message.toString()
+ ) as MessagePayload<DataPayload>
+ switch (type) {
+ case MessageType.echo:
+ ws.send(
+ JSON.stringify({
+ type: MessageType.echo,
+ data
+ })
+ )
+ break
+ case MessageType.factorial:
+ ws.send(
+ JSON.stringify({
+ type: MessageType.factorial,
+ data: { number: factorial(data.number as number) }
+ })
+ )
+ break
+ }
+ })
})
- })
- return {
- status: true,
- port: wss.options.port
+ return {
+ status: true,
+ port: WebSocketServerWorker.wss.options.port
+ }
}
-}
-class WebSocketServerWorker extends ClusterWorker<WorkerData, WorkerResponse> {
public constructor () {
- super(startWebSocketServer)
+ super(WebSocketServerWorker.startWebSocketServer)
}
}
ClusterWorkerData,
ClusterWorkerResponse
> {
+ private static wss: WebSocketServer
+ private static requestHandlerPool: DynamicThreadPool<
+ ThreadWorkerData<DataPayload>,
+ ThreadWorkerResponse<DataPayload>
+ >
+
private static readonly startWebSocketServer = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
const { port } = workerData as ClusterWorkerData
- const wss = new WebSocketServer({ port }, () => {
+ WebSocketServerWorker.wss = new WebSocketServer({ port }, () => {
console.info(
`⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/`
)
workerData?.workerFile as string
)
- wss.on('connection', ws => {
+ WebSocketServerWorker.wss.on('connection', ws => {
ws.on('error', console.error)
ws.on('message', (message: RawData) => {
const { type, data } = JSON.parse(
})
return {
status: true,
- port: wss.options.port
+ port: WebSocketServerWorker.wss.options.port
}
}
- private static requestHandlerPool: DynamicThreadPool<
- ThreadWorkerData<DataPayload>,
- ThreadWorkerResponse<DataPayload>
- >
-
public constructor () {
super(WebSocketServerWorker.startWebSocketServer)
}