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 | 80 | ws.send( |
66f0c14c JB |
81 | JSON.stringify( |
82 | { | |
83 | type: MessageType.factorial, | |
84 | data: response.data | |
85 | }, | |
86 | (_, v) => (typeof v === 'bigint' ? v.toString() : v) | |
87 | ) | |
8ff2efc7 | 88 | ) |
fefd3cef | 89 | return undefined |
8ff2efc7 JB |
90 | }) |
91 | .catch(emptyFunction) | |
92 | break | |
93 | } | |
94 | }) | |
95 | }) | |
96 | return { | |
97 | status: true, | |
3d49c6d2 | 98 | port: WebSocketServerWorker.wss.options.port |
8ff2efc7 JB |
99 | } |
100 | } | |
101 | ||
02999424 | 102 | public constructor () { |
5daad283 | 103 | super(WebSocketServerWorker.startWebSocketServer, { |
cb5d91cf | 104 | killHandler: async () => { |
cb5d91cf | 105 | await WebSocketServerWorker.requestHandlerPool.destroy() |
93b097ac | 106 | WebSocketServerWorker.wss.close() |
5daad283 JB |
107 | } |
108 | }) | |
02999424 JB |
109 | } |
110 | } | |
111 | ||
112 | export const webSocketServerWorker = new WebSocketServerWorker() |