Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: packages/isolate/example/http_server.dart

Issue 2990843002: Removed fixed dependencies (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/isolate/codereview.settings ('k') | packages/isolate/example/runner_pool.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library isolate.example.http_server;
6
7 import "dart:async";
8 import "dart:io";
9 import "dart:isolate";
10
11 import 'package:isolate/isolate_runner.dart';
12 import "package:isolate/ports.dart";
13 import "package:isolate/runner.dart";
14
15 typedef Future RemoteStop();
16
17 Future<RemoteStop> runHttpServer(
18 Runner runner, int port, HttpListener listener) async {
19 var stopPort = await runner.run(_startHttpServer, [port, listener]);
20
21 return () => _sendStop(stopPort);
22 }
23
24 Future _sendStop(SendPort stopPort) => singleResponseFuture(stopPort.send);
25
26 Future<SendPort> _startHttpServer(List args) async {
27 int port = args[0];
28 HttpListener listener = args[1];
29
30 var server =
31 await HttpServer.bind(InternetAddress.ANY_IP_V6, port, shared: true);
32 await listener.start(server);
33
34 return singleCallbackPort((SendPort resultPort) {
35 sendFutureResult(new Future.sync(listener.stop), resultPort);
36 });
37 }
38
39 /// An [HttpRequest] handler setup. Gets called when with the server, and
40 /// is told when to stop listening.
41 ///
42 /// These callbacks allow the listener to set up handlers for HTTP requests.
43 /// The object should be sendable to an equivalent isolate.
44 abstract class HttpListener {
45 Future start(HttpServer server);
46 Future stop();
47 }
48
49 /// An [HttpListener] that sets itself up as an echo server.
50 ///
51 /// Returns the message content plus an ID describing the isolate that
52 /// handled the request.
53 class EchoHttpListener implements HttpListener {
54 static const _delay = const Duration(seconds: 2);
55 static final _id = Isolate.current.hashCode;
56 final SendPort _counter;
57
58 StreamSubscription _subscription;
59
60 EchoHttpListener(this._counter);
61
62 Future start(HttpServer server) async {
63 print("Starting isolate $_id");
64 _subscription = server.listen((HttpRequest request) async {
65 await request.response.addStream(request);
66 print("Request to $hashCode");
67 request.response.write("#$_id\n");
68 var watch = new Stopwatch()..start();
69 while (watch.elapsed < _delay);
70 print("Response from $_id");
71 await request.response.close();
72 _counter.send(null);
73 });
74 }
75
76 Future stop() async {
77 print("Stopping isolate $_id");
78 await _subscription.cancel();
79 _subscription = null;
80 }
81 }
82
83 main(List<String> args) async {
84 int port = 0;
85 if (args.length > 0) {
86 port = int.parse(args[0]);
87 }
88
89 var counter = new ReceivePort();
90 HttpListener listener = new EchoHttpListener(counter.sendPort);
91
92 // Used to ensure the requested port is available or to find an available
93 // port if `0` is provided.
94 ServerSocket socket =
95 await ServerSocket.bind(InternetAddress.ANY_IP_V6, port, shared: true);
96
97 port = socket.port;
98 var isolates = await Future.wait(
99 new Iterable.generate(5, (_) => IsolateRunner.spawn()),
100 cleanUp: (isolate) {
101 isolate.close();
102 });
103
104 List<RemoteStop> stoppers = await Future.wait(isolates
105 .map((IsolateRunner isolate) {
106 return runHttpServer(isolate, socket.port, listener);
107 }), cleanUp: (server) {
108 server.stop();
109 });
110
111 await socket.close();
112 int count = 25;
113
114 print("Server listening on port $port for $count requests");
115 print("Test with:");
116 print(" ab -l -c10 -n $count http://localhost:$port/");
117
118 await for (var event in counter) {
119 count--;
120 if (count == 0) {
121 print('Shutting down');
122 for (var stopper in stoppers) {
123 await stopper();
124 }
125 counter.close();
126 }
127 }
128 print('Finished');
129 }
OLDNEW
« no previous file with comments | « packages/isolate/codereview.settings ('k') | packages/isolate/example/runner_pool.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698