kartikk221 / hyper-express

High performance Node.js webserver with a simple-to-use API powered by uWebsockets.js under the hood.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No error on binding to the same port

jesusgp22 opened this issue · comments

Hi, I'm trying out this library and I've found some odd behavior when trying to bind two processes to the same port, the first process to run will properly bind and serve requests, but the second won't error in any way

here is my code:

export default async function createServer() {
  const webserver = new Server();

  try {
    webserver.set_error_handler((req, res, error) => {
      logger.error({ req, res, error }, 'error happened!');
    });
    const socket = await webserver.listen(PORT, undefined).catch(error => {
      logger.error(error);
    });
    logger.info(socket); // returns nothing
    logger.info(`Webserver started on port ${PORT}`);
  } catch (err) {
    logger.error(err, `Failed to start webserver on port ${PORT}`);
  }

  return webserver;
}

running two process at the same time, one properly binds and serves requests, the other one fails silently:
image

maybe I am not handling errors properly?

Hey, so few things on this:

  • Binding on the same port will only work If the two processes your are launching as child processes with either worker threads or the cluster module in Node.js
  • Since uWebsockets.js works outside of the Node ecosystem through a C++ addon, I'd assume it possibly allows multiple processes to bind to the same port hence no failure
  • The second process is likely not silently failing but rather you aren't stressing HTTP requests on the port hard enough that uWebsockets.js and the operating system are just not routing any of the requests to the second process. Keep in mind, when you have multiple processes listen on the same port, there is no guarantee of a round robin approach for routing requests, its based on stress and metrics I believe.

What I would do going forward is try this out with child processes and separate processes and then use something like autocannon to send a lot of HTTP requests to the port to see If both processes handle the requests.