Index: tools/gardening/lib/src/util.dart |
diff --git a/tools/gardening/lib/src/util.dart b/tools/gardening/lib/src/util.dart |
index 51b50c1e3dec79d037401a16254d3a4046ee07ee..108fffcb98d4867c62e33d22b2c5b99fb1202bb1 100644 |
--- a/tools/gardening/lib/src/util.dart |
+++ b/tools/gardening/lib/src/util.dart |
@@ -64,15 +64,39 @@ class HttpException implements Exception { |
String toString() => '$uri: $statusCode'; |
} |
+class TimeoutException implements Exception { |
floitsch
2017/08/15 11:26:35
I would have used the builtin TimeoutException cla
Johnni Winther
2017/08/15 12:51:27
Done.
|
+ final Uri uri; |
+ |
+ TimeoutException(this.uri); |
+ |
+ String toString() => '$uri timeout'; |
+} |
+ |
/// Reads the content of [uri] as text. |
-Future<String> readUriAsText(HttpClient client, Uri uri) async { |
+Future<String> readUriAsText( |
+ HttpClient client, Uri uri, Duration timeout) async { |
HttpClientRequest request = await client.getUrl(uri); |
HttpClientResponse response = await request.close(); |
if (response.statusCode != 200) { |
response.drain(); |
throw new HttpException(uri, response.statusCode); |
} |
- return response.transform(UTF8.decoder).join(); |
+ List<int> data = <int>[]; |
+ Timer timer; |
+ Completer<String> completer = new Completer<String>(); |
+ StreamSubscription subscription = response.listen((List<int> buffer) { |
floitsch
2017/08/15 11:26:35
You could consider using .timeout from the Stream
Johnni Winther
2017/08/15 12:51:27
Done.
|
+ data.addAll(buffer); |
+ }, onDone: () { |
+ timer?.cancel(); |
+ completer.complete(UTF8.decode(data)); |
+ }, cancelOnError: true); |
+ if (timeout != null) { |
+ timer = new Timer(timeout, () { |
+ subscription.cancel(); |
+ completer.completeError(new TimeoutException(uri)); |
+ }); |
+ } |
+ return completer.future; |
} |
ArgParser createArgParser() { |