Hi all,
With the release of Dart 1.4, we've added a new experimental feature to dart:io, ServerSocket references.
A ServerSocket reference can be acquired from a ServerSocket and can be transferred to other isolates. From this reference, it's possibly to create a clone of the original ServerSocket, that will share the native socket. This makes it possible to accept incoming connection on multiple isolates, on the same network interface.
Currently, there are a few important notes about this new feature.
- It is VERY experimental, and is most likely to have its API changed.
- It is only implemented on Linux for now. There is no technical limitation to porting it to other platforms, but we want to get the API right before finalizing the implementation.
Example:
A very simple HTTP server could look like this:
void listen(HttpServer server) {
server.listen((HttpRequest request) {
request.response.write("Hello, world");
request.response.close();
});
}
void main() {
HttpServer.bind('127.0.0.1', 8080).then(listen);
}
This will start a HTTP server on the main isolate.
Running wrk[1] as follows
results in around 25,000 req/sec.
Now, using ServerSocket references, we can scale this to multiple isolates:
void handle(reference) {
// Create a ServerSocket from the reference.
reference.create().then((serverSocket) {
listen(new HttpServer.listenOn(serverSocket));
});
}
void main() {
ServerSocket.bind('127.0.0.1', 8080).then((server) {
// Get a reference to the server socket.
var ref = server.reference;
for (int i = 1; i < 8; i++) {
// Spawn isolate with 'ref' as argument.
Isolate.spawn(handle, ref);
}
listen(new HttpServer.listenOn(server));
});
}
Here we send the reference to 7 new isolates, listening to the server-socket in a total of 8 isolates. Running the same 'wrk' command as above now gives around 200,000 req/sec.
This is an important new feature for making isolates more valuable in the context of dart:io. We hope that you will try it out and provide feedback, here or at
http://dartbug.com/new, to help us evolve this API in the right direction.
Thank you!,
- Anders