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