How to run Angel in multiple isolates.
The concept is pretty simple. A normal server would look like this:
typedef Future<HttpServer> ServerGenerator(InternetAddress address, int port);
new Angel.custom((address, port) => HttpServer.bind(address, port, shared: true));
new Angel.custom(startShared);
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:angel_framework/angel_framework.dart';
main(List<String> args) async {
int concurrency = Platform.numberOfProcessors;
// Start child isolates...
for (int i = 1; i < concurrency; i++) {
Isolate.spawn(serverMain, i);
}
// Spawn a server in the main isolate.
serverMain(concurrency);
}
void serverMain(int id) {
// Start shared!!!
var app = new Angel();
var http = new AngelHttp.custom(app, startShared);
app.get('/json', () => {'hello': 'world'});
app.get('/db', () async {
// Run a query...
var connection = new PostgreSQLConnection(
'127.0.0.1', 5432, 'wrk_benchmark',
username: 'postgres', password: 'password');
await connection.open();
var rows = await connection.query('SELECT id, text from notes;');
return rows.map((row) => {'id': row[0], 'text': row[1]});
});
http.startServer(InternetAddress.ANY_IP_V4, 3000).then((server) {
print(
'Instance #$id listening at http://${server.address.address}:${server.port}');
});
}