Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: Worker based implementation? #37449

Open
ronag opened this issue Feb 20, 2021 · 1 comment
Open

cluster: Worker based implementation? #37449

ronag opened this issue Feb 20, 2021 · 1 comment
Labels
cluster Issues and PRs related to the cluster subsystem. question Issues that look for answers.

Comments

@ronag
Copy link
Member

ronag commented Feb 20, 2021

Would it make sense/be possible to re-implement/create alternative implementation for cluster that is based on Workers instead of child processes? Would make it possible to more efficiently pass data between the parent process and workers.

@ronag ronag added cluster Issues and PRs related to the cluster subsystem. question Issues that look for answers. labels Feb 20, 2021
@ljluestc
Copy link

const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const http = require('http');

if (isMainThread) {
  // This is the main thread (master process).
  const numWorkers = 4;
  const workers = [];

  // Create worker threads.
  for (let i = 0; i < numWorkers; i++) {
    const worker = new Worker(__filename, { workerData: { workerId: i } });
    workers.push(worker);
  }

  // Create a simple HTTP server to distribute requests.
  const server = http.createServer((req, res) => {
    // Distribute incoming HTTP requests among workers.
    const worker = workers[Math.floor(Math.random() * numWorkers)];
    worker.postMessage(req.url);
  });

  server.listen(3000, () => {
    console.log('Server is running on port 3000');
  });
} else {
  // This is a worker thread.
  const workerId = workerData.workerId;

  parentPort.on('message', (url) => {
    // Handle the request in the worker thread.
    console.log(`Worker ${workerId} processing request for URL: ${url}`);

    // Simulate some work (e.g., processing the request).
    setTimeout(() => {
      console.log(`Worker ${workerId} finished processing request for URL: ${url}`);
      parentPort.postMessage(`Response from Worker ${workerId} for URL: ${url}`);
    }, 1000);
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cluster Issues and PRs related to the cluster subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

2 participants