Release resources for requests that error out. (#2)
This was causing dart-lang/pub#1610
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..0e4203b
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,7 @@
+## 1.0.1
+
+* Properly release resources for requests that error out.
+
+## 1.0.0
+
+* Initial release.
diff --git a/lib/http_throttle.dart b/lib/http_throttle.dart
index a9e9e50..5e30ed8 100644
--- a/lib/http_throttle.dart
+++ b/lib/http_throttle.dart
@@ -25,23 +25,29 @@
: _pool = new Pool(maxActiveRequests),
_inner = inner == null ? new Client() : inner;
- Future<StreamedResponse> send(BaseRequest request) {
- return _pool.request().then((resource) {
- return _inner.send(request).then((response) {
- var stream = response.stream.transform(
- new StreamTransformer.fromHandlers(handleDone: (sink) {
- resource.release();
- sink.close();
- }));
- return new StreamedResponse(stream, response.statusCode,
- contentLength: response.contentLength,
- request: response.request,
- headers: response.headers,
- isRedirect: response.isRedirect,
- persistentConnection: response.persistentConnection,
- reasonPhrase: response.reasonPhrase);
- });
- });
+ Future<StreamedResponse> send(BaseRequest request) async {
+ var resource = await _pool.request();
+
+ StreamedResponse response;
+ try {
+ response = await _inner.send(request);
+ } catch (_) {
+ resource.release();
+ rethrow;
+ }
+
+ var stream = response.stream.transform(
+ new StreamTransformer.fromHandlers(handleDone: (sink) {
+ resource.release();
+ sink.close();
+ }));
+ return new StreamedResponse(stream, response.statusCode,
+ contentLength: response.contentLength,
+ request: response.request,
+ headers: response.headers,
+ isRedirect: response.isRedirect,
+ persistentConnection: response.persistentConnection,
+ reasonPhrase: response.reasonPhrase);
}
void close() => _inner.close();
diff --git a/pubspec.yaml b/pubspec.yaml
index 1cb3291..9d0817a 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: http_throttle
-version: 1.0.1-dev
+version: 1.0.1
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/http_throttle
description: HTTP client middleware that throttles requests.
diff --git a/test/http_throttle_test.dart b/test/http_throttle_test.dart
index 0cf0662..226e763 100644
--- a/test/http_throttle_test.dart
+++ b/test/http_throttle_test.dart
@@ -54,6 +54,18 @@
expect(pendingResponses, hasLength(5));
});
});
+
+ test("releases resources when HTTP requests error out", () {
+ var pendingResponses = [];
+ var client = new ThrottleClient(10,
+ new MockClient((request) => new Future.error("oh no!")));
+
+ // Every request should throw. If we aren't properly releasing resources,
+ // all of these after the 10th will fail to complete.
+ for (var i = 0; i < 20; i++) {
+ expect(client.get('/'), throwsA("oh no!"));
+ }
+ });
}
/// Returns a [Future] that completes after pumping the event queue [times]