mafintosh / turbo-http

Blazing fast low level http server
MIT License
1k stars 47 forks source link

[Question] Cluster support planned? #11

Open asciidisco opened 6 years ago

asciidisco commented 6 years ago

Hi there. First of all, turbo-net & turbo-http are amazing, thank you very much :)

If I´m not mistaken, turbo-http doesn't work in cluster mode yet, as it doesn't implement the same "behind the scenes magick" as the default http module does to "share" an open port.

I´ve just, very naively, tried to map the example from the docs 1:1 - https://nodejs.org/docs/latest/api/cluster.html

So, chances are high that I´m just doing something wrong here, but if not, I´d just simply ask if it is planned to implement cluster compat., or if I should go down the route of forking instances & using a traditional load balancer instead of cluster mode?

jacktuck commented 6 years ago

If I´m not mistaken, turbo-http doesn't work in cluster mode yet, as it doesn't implement the same "behind the scenes magick"

I think you're right here, just ran into this myself too. So +1

mafintosh commented 6 years ago

Yes turbo-net does not do any clustering. I'm not sure what's involved to get that working.

asciidisco commented 6 years ago

@mafintosh Thank you for the answer, it´s not that critical, as I don't want to use it for a production product yet & I´m just toying around with it.

I´ll see if I can find some clues in the node sources, but I doubt that this is a trivial implementation.

jacktuck commented 6 years ago

haha @asciidisco i've been doing exactly that and... it's been quite the rabbit hole

e.g. https://github.com/nodejs/node/blob/212de3c5ec429a580d2e79ce3c2516b93b52b8f5/lib/_http_common.js#L264 😱

asciidisco commented 6 years ago

@jacktuck https://github.com/nodejs/node/blob/master/lib/internal/cluster/round_robin_handle.js

asciidisco commented 6 years ago

The way it is implemented in node is quite understandable. It looks like one could create a separate package that could just wrap turbo-http and make it cluster-able.

If I´m not missing any important point, I´d say that if it can be done as a separate package, it should be done this way, as every implementation in turbo-http would have a performance impact of some sort, even if users wouldn't need the functionality.

Therefore I´m fine with closing this. If >1 instances are needed, people could still use the "classic" load balancer approach.

jacktuck commented 6 years ago

@asciidisco

The way it is implemented in node is quite understandable

Can you give me synopsis of how node's doing it? I think your link relates to distributing workers onto 'random' ports rather than any kind of port sharing between the workers. Might be wrong though.

Yeah i also wonder what the performance hit would be

asciidisco commented 6 years ago

@jacktuck It´s explained in the docs itself (better than I could ever do it): https://nodejs.org/api/cluster.html#cluster_how_it_works

aichholzer commented 5 years ago

If you need clustering, you can use @rayo/storm which will basically spawn your worker (server) function over all available CPU cores (you can also limit the number) and even give you a nice (still basic) cluster/worker monitoring service. 👏

Something along the lines of:

const rayo = require('rayo');
const turbo = require('turbo-http');
const server = turbo.createServer();

rayo({ port: 5050, server, storm: {} })
  .get('/', (req, res) => res.end('Thunderstruck by rayo, turbo and storm..!'))
  .start((address) => {
    console.log(`Up on port ${address.port}`);
  });

In this case rayo.js will, essentially, just provide the routing capabilities. Also, in this example, you don't need to require @rayo/storm since rayo already comes bundled with it, you just need to turn it on.

jfrconley commented 5 years ago

I submitted a pull request to turbo-net that enables cluster support. https://github.com/mafintosh/turbo-net/pull/16

IonelLupu commented 5 years ago

Hi. First of all, great work here. Are there any updates on this?

dalisoft commented 5 years ago

Any news?