From ccb3cc9ed21a35ecdcba529a704211cf78b81337 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 24 Dec 2016 13:17:32 -0500 Subject: [PATCH] Added multiserve support --- .vscode/launch.json | 9 +++++++++ bin/multi_server.dart | 42 ++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 1 + 3 files changed, 52 insertions(+) create mode 100644 bin/multi_server.dart diff --git a/.vscode/launch.json b/.vscode/launch.json index d23dcd5..d6b9b24 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,6 +9,15 @@ "debugSettings": "${command.debugSettings}", "program": "${workspaceRoot}/bin/server.dart", "args": [] + }, + { + "name": "Start Multiserver", + "type": "dart-cli", + "request": "launch", + "cwd": "${workspaceRoot}", + "debugSettings": "${command.debugSettings}", + "program": "${workspaceRoot}/bin/multi_server.dart", + "args": [] } ] } \ No newline at end of file diff --git a/bin/multi_server.dart b/bin/multi_server.dart new file mode 100644 index 0000000..3960384 --- /dev/null +++ b/bin/multi_server.dart @@ -0,0 +1,42 @@ +import 'dart:convert'; +import 'dart:io'; +import 'package:angel_multiserver/angel_multiserver.dart'; + +final Uri cluster = Platform.script.resolve('cluster.dart'); +final errorPage = GZIP.encode(UTF8.encode(''' + + + + 503 Service Unavailable + + +

503 Service Unavailable

+ There is no server available to service your request. + + + ''')); + +main() async { + var loadBalancer = new LoadBalancer(); + await loadBalancer.spawnIsolates(cluster, count: 3); + + loadBalancer + ..onCrash.listen((_) { + // Auto-spawn a new instance on crash + loadBalancer.spawnIsolates(cluster); + }) + ..onUnavailable.listen((socket) async { + socket + ..writeln('HTTP/1.1 503 Service Unavailable') + ..writeln(HttpDate.format(new DateTime.now())) + ..writeln('Content-Encoding: gzip') + ..writeln() + ..add(errorPage) + ..writeln(); + await socket.close(); + }); + + var server = await loadBalancer.startServer(); + print( + 'Load balancer listening at http://${server.address.address}:${server.port}'); +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 0eaba75..7b0c568 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,6 +7,7 @@ homepage: https://github.com/angel-dart/angel dependencies: angel_common: ^1.0.0-alpha angel_configuration: ^1.0.0 + angel_multiserver: ^1.0.0-dev mailer: ^1.1.0+4 validate: ^1.5.2 dev_dependencies: