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

Side by Side Diff: example/http_server.dart

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

Powered by Google App Engine
This is Rietveld 408576698