Commit | Line | Data |
---|---|---|
8ff2efc7 | 1 | import { |
ded253e2 | 2 | availableParallelism, |
8ff2efc7 | 3 | ClusterWorker, |
ded253e2 | 4 | DynamicThreadPool |
8ff2efc7 | 5 | } from 'poolifier' |
02999424 | 6 | import { type RawData, WebSocketServer } from 'ws' |
ded253e2 | 7 | |
02999424 JB |
8 | import { |
9 | type ClusterWorkerData, | |
10 | type ClusterWorkerResponse, | |
11 | type DataPayload, | |
12 | type MessagePayload, | |
8ff2efc7 JB |
13 | MessageType, |
14 | type ThreadWorkerData, | |
15 | type ThreadWorkerResponse | |
02999424 | 16 | } from './types.js' |
02999424 JB |
17 | |
18 | const emptyFunction = (): void => { | |
8ea47589 | 19 | /* Intentional */ |
02999424 JB |
20 | } |
21 | ||
02999424 JB |
22 | class WebSocketServerWorker extends ClusterWorker< |
23 | ClusterWorkerData, | |
24 | ClusterWorkerResponse | |
25 | > { | |
3d49c6d2 JB |
26 | private static wss: WebSocketServer |
27 | private static requestHandlerPool: DynamicThreadPool< | |
28 | ThreadWorkerData<DataPayload>, | |
29 | ThreadWorkerResponse<DataPayload> | |
30 | > | |
31 | ||
8ff2efc7 JB |
32 | private static readonly startWebSocketServer = ( |
33 | workerData?: ClusterWorkerData | |
34 | ): ClusterWorkerResponse => { | |
7d9756e3 | 35 | const { port, workerFile, minWorkers, maxWorkers, ...poolOptions } = |
67f3f2d6 | 36 | workerData! |
8ff2efc7 | 37 | |
63ff88a9 JB |
38 | WebSocketServerWorker.requestHandlerPool = new DynamicThreadPool< |
39 | ThreadWorkerData<DataPayload>, | |
40 | ThreadWorkerResponse<DataPayload> | |
41 | >( | |
d0ed34c9 JB |
42 | minWorkers ?? 1, |
43 | maxWorkers ?? availableParallelism(), | |
44 | workerFile, | |
7d9756e3 | 45 | poolOptions |
63ff88a9 JB |
46 | ) |
47 | ||
d0ed34c9 JB |
48 | WebSocketServerWorker.wss = new WebSocketServer({ port }, () => { |
49 | console.info( | |
50 | `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/` | |
51 | ) | |
52 | }) | |
53 | ||
041dc05b | 54 | WebSocketServerWorker.wss.on('connection', ws => { |
8ff2efc7 JB |
55 | ws.on('error', console.error) |
56 | ws.on('message', (message: RawData) => { | |
57 | const { type, data } = JSON.parse( | |
58 | // eslint-disable-next-line @typescript-eslint/no-base-to-string | |
59 | message.toString() | |
60 | ) as MessagePayload<DataPayload> | |
61 | switch (type) { | |
62 | case MessageType.echo: | |
63ff88a9 | 63 | WebSocketServerWorker.requestHandlerPool |
8ff2efc7 | 64 | .execute({ data }, 'echo') |
041dc05b | 65 | .then(response => { |
8ff2efc7 JB |
66 | ws.send( |
67 | JSON.stringify({ | |
68 | type: MessageType.echo, | |
69 | data: response.data | |
70 | }) | |
71 | ) | |
fefd3cef | 72 | return undefined |
8ff2efc7 JB |
73 | }) |
74 | .catch(emptyFunction) | |
75 | break | |
76 | case MessageType.factorial: | |
63ff88a9 | 77 | WebSocketServerWorker.requestHandlerPool |
8ff2efc7 | 78 | .execute({ data }, 'factorial') |
041dc05b | 79 | .then(response => { |
8ff2efc7 JB |
80 | ws.send( |
81 | JSON.stringify({ | |
82 | type: MessageType.factorial, | |
83 | data: response.data | |
84 | }) | |
85 | ) | |
fefd3cef | 86 | return undefined |
8ff2efc7 JB |
87 | }) |
88 | .catch(emptyFunction) | |
89 | break | |
90 | } | |
91 | }) | |
92 | }) | |
93 | return { | |
94 | status: true, | |
3d49c6d2 | 95 | port: WebSocketServerWorker.wss.options.port |
8ff2efc7 JB |
96 | } |
97 | } | |
98 | ||
02999424 | 99 | public constructor () { |
5daad283 | 100 | super(WebSocketServerWorker.startWebSocketServer, { |
cb5d91cf | 101 | killHandler: async () => { |
cb5d91cf | 102 | await WebSocketServerWorker.requestHandlerPool.destroy() |
93b097ac | 103 | WebSocketServerWorker.wss.close() |
5daad283 JB |
104 | } |
105 | }) | |
02999424 JB |
106 | } |
107 | } | |
108 | ||
109 | export const webSocketServerWorker = new WebSocketServerWorker() |