Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Video-Downloader/api/src/misc/cluster.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
71 lines (59 sloc)
1.66 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cluster from "node:cluster"; | |
import net from "node:net"; | |
import { syncSecrets } from "../security/secrets.js"; | |
import { env, isCluster } from "../config.js"; | |
export { isPrimary, isWorker } from "node:cluster"; | |
export const supportsReusePort = async () => { | |
try { | |
await new Promise((resolve, reject) => { | |
const server = net.createServer().listen({ port: 0, reusePort: true }); | |
server.on('listening', () => server.close(resolve)); | |
server.on('error', (err) => (server.close(), reject(err))); | |
}); | |
return true; | |
} catch { | |
return false; | |
} | |
} | |
export const initCluster = async () => { | |
if (cluster.isPrimary) { | |
for (let i = 1; i < env.instanceCount; ++i) { | |
cluster.fork(); | |
} | |
} | |
await syncSecrets(); | |
} | |
export const broadcast = (message) => { | |
if (!isCluster || !cluster.isPrimary || !cluster.workers) { | |
return; | |
} | |
for (const worker of Object.values(cluster.workers)) { | |
worker.send(message); | |
} | |
} | |
export const send = (message) => { | |
if (!isCluster) { | |
return; | |
} | |
if (cluster.isPrimary) { | |
return broadcast(message); | |
} else { | |
return process.send(message); | |
} | |
} | |
export const waitFor = (key) => { | |
return new Promise(resolve => { | |
const listener = (message) => { | |
if (key in message) { | |
process.off('message', listener); | |
return resolve(message); | |
} | |
} | |
process.on('message', listener); | |
}); | |
} | |
export const mainOnMessage = (cb) => { | |
for (const worker of Object.values(cluster.workers)) { | |
worker.on('message', cb); | |
} | |
} |