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

Side by Side Diff: runtime/bin/vmservice/server.dart

Issue 2772463003: [vm service] Have HTTP server retry binding for 10 seconds (Closed)
Patch Set: Created 3 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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 part of vmservice_io; 5 part of vmservice_io;
6 6
7 final bool silentObservatory = const bool.fromEnvironment('SILENT_OBSERVATORY'); 7 final bool silentObservatory = const bool.fromEnvironment('SILENT_OBSERVATORY');
8 8
9 void serverPrint(String s) { 9 void serverPrint(String s) {
10 if (silentObservatory) { 10 if (silentObservatory) {
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 } 316 }
317 } 317 }
318 318
319 Future startup() async { 319 Future startup() async {
320 if (_server != null) { 320 if (_server != null) {
321 // Already running. 321 // Already running.
322 return this; 322 return this;
323 } 323 }
324 324
325 // Startup HTTP server. 325 // Startup HTTP server.
326 try { 326 var pollError;
327 var address; 327 var pollStack;
328 if (Platform.isFuchsia) { 328 Future<bool> poll() async {
329 address = InternetAddress.ANY_IP_V4; 329 try {
330 } else { 330 var address;
331 var addresses = await InternetAddress.lookup(_ip); 331 if (Platform.isFuchsia) {
332 // Prefer IPv4 addresses. 332 address = InternetAddress.ANY_IP_V4;
333 for (var i = 0; i < addresses.length; i++) { 333 } else {
334 address = addresses[i]; 334 var addresses = await InternetAddress.lookup(_ip);
335 if (address.type == InternetAddressType.IP_V4) break; 335 // Prefer IPv4 addresses.
336 for (var i = 0; i < addresses.length; i++) {
337 address = addresses[i];
338 if (address.type == InternetAddressType.IP_V4) break;
339 }
336 } 340 }
341 _server = await HttpServer.bind(address, _port);
342 return true;
343 } catch (e, st) {
344 pollError = e;
345 pollStack = st;
346 return false;
337 } 347 }
338 _server = await HttpServer.bind(address, _port); 348 }
339 _server.listen(_requestHandler, cancelOnError: true); 349
340 serverPrint('Observatory listening on $serverAddress'); 350 // poll for the network for ~10 seconds.
341 if (Platform.isFuchsia) { 351 int attempts = 0;
342 // Create a file with the port number. 352 final int maxAttempts = 10;
343 String tmp = Directory.systemTemp.path; 353 while (!await poll()) {
344 String path = "$tmp/dart.services/${_server.port}"; 354 attempts++;
345 serverPrint("Creating $path"); 355 serverPrint("Observatory server failed to start after $attempts tries");
346 new File(path)..createSync(recursive: true); 356 if (attempts > maxAttempts) {
357 serverPrint('Could not start Observatory HTTP server:\n'
358 '$pollError\n$pollStack\n');
359 _notifyServerState("");
360 onServerAddressChange(null);
361 return this;
347 } 362 }
348 // Server is up and running. 363 await new Future<Null>.delayed(const Duration(seconds: 1));
349 _notifyServerState(serverAddress.toString());
350 onServerAddressChange('$serverAddress');
351 return this;
352 } catch (e, st) {
353 serverPrint('Could not start Observatory HTTP server:\n$e\n$st\n');
354 _notifyServerState("");
355 onServerAddressChange(null);
356 return this;
357 } 364 }
365 _server.listen(_requestHandler, cancelOnError: true);
366 serverPrint('Observatory listening on $serverAddress');
367 if (Platform.isFuchsia) {
368 // Create a file with the port number.
369 String tmp = Directory.systemTemp.path;
370 String path = "$tmp/dart.services/${_server.port}";
371 serverPrint("Creating $path");
372 new File(path)..createSync(recursive: true);
373 }
374 // Server is up and running.
375 _notifyServerState(serverAddress.toString());
376 onServerAddressChange('$serverAddress');
377 return this;
358 } 378 }
359 379
360 Future cleanup(bool force) { 380 Future cleanup(bool force) {
361 if (_server == null) { 381 if (_server == null) {
362 return new Future.value(null); 382 return new Future.value(null);
363 } 383 }
364 if (Platform.isFuchsia) { 384 if (Platform.isFuchsia) {
365 // Remove the file with the port number. 385 // Remove the file with the port number.
366 String tmp = Directory.systemTemp.path; 386 String tmp = Directory.systemTemp.path;
367 String path = "$tmp/dart.services/${_server.port}"; 387 String path = "$tmp/dart.services/${_server.port}";
(...skipping 21 matching lines...) Expand all
389 _server = null; 409 _server = null;
390 serverPrint('Could not shutdown Observatory HTTP server:\n$e\n$st\n'); 410 serverPrint('Could not shutdown Observatory HTTP server:\n$e\n$st\n');
391 _notifyServerState(""); 411 _notifyServerState("");
392 onServerAddressChange(null); 412 onServerAddressChange(null);
393 return this; 413 return this;
394 }); 414 });
395 } 415 }
396 } 416 }
397 417
398 void _notifyServerState(String uri) native "VMServiceIO_NotifyServerState"; 418 void _notifyServerState(String uri) native "VMServiceIO_NotifyServerState";
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