()
| 10 | function ignoreError() {} |
| 11 | |
| 12 | export function setupPrimary() { |
| 13 | cluster.on("message", (sourceWorker, message: { _source?: string }) => { |
| 14 | if (message._source !== MESSAGE_SOURCE) { |
| 15 | debug("ignore message from unknown source"); |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | if (!sourceWorker[kNodeId]) { |
| 20 | sourceWorker[kNodeId] = (message as Message).senderId; |
| 21 | } |
| 22 | |
| 23 | // @ts-expect-error recipientId is not defined for all messages |
| 24 | let recipientId = (message as Message).recipientId; |
| 25 | if (recipientId) { |
| 26 | for (const worker of Object.values(cluster.workers)) { |
| 27 | if (worker[kNodeId] === recipientId) { |
| 28 | debug("forward message to worker %d", worker.id); |
| 29 | worker.send(message, null, ignoreError); |
| 30 | return; |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | debug("forward message to all other workers"); |
| 36 | for (const worker of Object.values(cluster.workers)) { |
| 37 | if (worker.id !== sourceWorker.id) { |
| 38 | worker.send(message, null, ignoreError); |
| 39 | } |
| 40 | } |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | export class NodeClusterEngine extends ClusterEngine { |
| 45 | constructor(opts?: ServerOptions) { |
no test coverage detected