| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, 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 import 'dart:async'; |
| 6 import 'dart:convert'; |
| 7 import 'dart:math' as math; |
| 8 |
| 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/plugin/plugin_manager.dart'; |
| 11 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| 12 import 'package:analyzer_plugin/protocol/protocol.dart' as plugin; |
| 13 import 'package:analyzer_plugin/protocol/protocol_constants.dart' as plugin; |
| 14 import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin; |
| 15 import 'package:analyzer_plugin/src/protocol/protocol_internal.dart' as plugin; |
| 16 |
| 17 /** |
| 18 * An abstract implementation of a request handler. |
| 19 */ |
| 20 abstract class AbstractRequestHandler implements RequestHandler { |
| 21 /** |
| 22 * The analysis server that is using this handler to process requests. |
| 23 */ |
| 24 final AnalysisServer server; |
| 25 |
| 26 /** |
| 27 * Initialize a newly created request handler to be associated with the given |
| 28 * analysis [server]. |
| 29 */ |
| 30 AbstractRequestHandler(this.server); |
| 31 |
| 32 /** |
| 33 * Given a mapping from plugins to futures that will complete when the plugin |
| 34 * has responded to a request, wait for a finite amount of time for each of |
| 35 * the plugins to respond. Return a list of the responses from each of the |
| 36 * plugins. If a plugin fails to return a response, notify the plugin manager |
| 37 * associated with the server so that non-responsive plugins can be killed or |
| 38 * restarted. The [timeout] is the maximum amount of time that will be spent |
| 39 * waiting for plugins to respond. |
| 40 */ |
| 41 Future<List<plugin.Response>> waitForResponses( |
| 42 Map<PluginInfo, Future<plugin.Response>> futures, |
| 43 {plugin.RequestParams requestParameters, |
| 44 int timeout: 500}) async { |
| 45 int endTime = new DateTime.now().millisecondsSinceEpoch + timeout; |
| 46 List<plugin.Response> responses = <plugin.Response>[]; |
| 47 for (PluginInfo pluginInfo in futures.keys) { |
| 48 Future<plugin.Response> future = futures[pluginInfo]; |
| 49 try { |
| 50 int startTime = new DateTime.now().millisecondsSinceEpoch; |
| 51 plugin.Response response = await future.timeout( |
| 52 new Duration(milliseconds: math.max(endTime - startTime, 0))); |
| 53 if (response.error != null) { |
| 54 // TODO(brianwilkerson) Report the error to the plugin manager. |
| 55 server.instrumentationService.logPluginError( |
| 56 pluginInfo.data, |
| 57 response.error.code.name, |
| 58 response.error.message, |
| 59 response.error.stackTrace); |
| 60 } else { |
| 61 responses.add(response); |
| 62 } |
| 63 } on TimeoutException { |
| 64 // TODO(brianwilkerson) Report the timeout to the plugin manager. |
| 65 server.instrumentationService.logPluginTimeout( |
| 66 pluginInfo.data, |
| 67 new JsonEncoder() |
| 68 .convert(requestParameters.toRequest('-').toJson())); |
| 69 } catch (exception, stackTrace) { |
| 70 // TODO(brianwilkerson) Report the exception to the plugin manager. |
| 71 server.instrumentationService |
| 72 .logPluginException(pluginInfo.data, exception, stackTrace); |
| 73 } |
| 74 } |
| 75 return responses; |
| 76 } |
| 77 } |
| OLD | NEW |