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

Unified Diff: pkg/http_throttle/lib/http_throttle.dart

Issue 418103004: Add an http_throttle package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/http_throttle/README.md ('k') | pkg/http_throttle/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/http_throttle/lib/http_throttle.dart
diff --git a/pkg/http_throttle/lib/http_throttle.dart b/pkg/http_throttle/lib/http_throttle.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7cd95a6b40696a46bd4c03d1ff7d78e59598a28c
--- /dev/null
+++ b/pkg/http_throttle/lib/http_throttle.dart
@@ -0,0 +1,50 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library http_throttle;
+
+import 'dart:async';
+
+import 'package:http/http.dart';
+import 'package:pool/pool.dart';
+
+/// A middleware client that throttles the number of concurrent requests.
+///
+/// As long as the number of requests is within the limit, this works just like
+/// a normal client. If a request is made beyond the limit, the underlying HTTP
+/// request won't be sent until other requests have completed.
+class ThrottleClient extends BaseClient {
+ final Pool _pool;
+ final Client _inner;
+
+ /// Creates a new client that allows no more than [maxActiveRequests]
+ /// concurrent requests.
+ ///
+ /// If [inner] is passed, it's used as the inner client for sending HTTP
+ /// requests. It defaults to `new http.Client()`.
+ ThrottleClient(int maxActiveRequests, [Client inner])
+ : _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);
+ });
+ });
+ }
+
+ void close() => _inner.close();
+}
« no previous file with comments | « pkg/http_throttle/README.md ('k') | pkg/http_throttle/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698