If I have 4 Dart Server Running and I want to forward requests, how would I do that in Dart? On one hand I want to efficiently react on the requests but also be able to have certain requests handled in a distinct way or have an IP from a city handled somewhere. So I evaluate a session ID and say this session should be served by server2:1234 meaning the response ideally would be printed by that server, not routed everything through the server1:80 because that massively drains the availability of the server1:80
In "hello world" numbers:
server1:80 can serve aprox: 8000 req/sec
4 servers can serve aprox: 15-20000 req/sec (with nginx as a frontend)
Isolates can not handle httprequests afaik I would need to parse/downgrade the request for the isolate which is even worse.
2 questions:
How can I forward a request without blocking the main instance?
(like with nginx loadbalancing)
How can I ideally route requests to isolates?
(any example I found was either outdated or used a pattern that I would not prefere: spawning an isolate for every request... not a good Idea. I'd rather dynamically create server instances in Isolates and forward requests there)
The main issue I see is, that you don't share memory, so the question is, if it is even possible to route any data without having a duplicate copy. If servers run on different machines you would create serious traffic overhead. A method to dynamically header redirect/rewrite would be best I guess. But even for that I'd need lets say 4-16 "threads" on port 80 to be efficient. Afaik not possible with dart. Whats the best thing to do?
I'd really appreciate help here.
UPDATE
with this patch:
https://codereview.chromium.org/250513002/
you can actually achieve parallel server processes which works similar as an
older patch:
https://codereview.chromium.org/25511002/
in both cases you listen on a same port but in case two you can just run another process and reuse the port while in case one you can run processes in isolates and share a ServerSocketReference.
The older patch needed some adjustments but finally with the new SDK I can at least get rid of nginx. Saving the nginx proxy pass delivers arround 10-20 % more hello world requests. With 2 parallel processes I achieved around 140% performance while with 3 and 4 it was just 145%. But in total arround 10k req/sec on my laptop which is fine(also states we just want to die in beauty here ;) )
This is highly experimental and nobody knows how it will work in the future. I recommend the first patch since it seems to be the cleaner approach.
Here is the basic example I wished to find earlier :)
import 'dart:io';
import 'dart:isolate';
isoserve(List d){
d[0].create().then((server){
HttpServer httpserver = new HttpServer.listenOn(server);
httpserver.listen((HttpRequest hr){
hr.response.write(d[1]);
hr.response.close();
});
});
}
main() {
ServerSocket.bind(InternetAddress.ANY_IP_V6, 5555).then( (serverSocket) {
Isolate.spawn(isoserve,[serverSocket.reference,"aloha world"]);
Isolate.spawn(isoserve,[serverSocket.reference,"aloha world 2"]);
});
}
UPDATE == works with SDK 1.4 now