From a62ea0ead9735c6261b22169a95f56e3aa290152 Mon Sep 17 00:00:00 2001 From: Glenn Ruysschaert Date: Tue, 19 Oct 2021 17:06:30 +0200 Subject: [PATCH] Use a pool for connections --- lib/http/intercepted_client.dart | 19 ++++++++++++++----- pubspec.lock | 2 +- pubspec.yaml | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/http/intercepted_client.dart b/lib/http/intercepted_client.dart index 0997478..1ec6348 100644 --- a/lib/http/intercepted_client.dart +++ b/lib/http/intercepted_client.dart @@ -3,8 +3,9 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:http/http.dart'; -import 'package:http_interceptor/models/models.dart'; import 'package:http_interceptor/extensions/extensions.dart'; +import 'package:http_interceptor/models/models.dart'; +import 'package:pool/pool.dart'; import 'http_methods.dart'; import 'interceptor_contract.dart'; @@ -42,6 +43,7 @@ class InterceptedClient extends BaseClient { int _retryCount = 0; late Client _inner; + late Pool _pool; InterceptedClient._internal({ required this.interceptors, @@ -49,7 +51,9 @@ class InterceptedClient extends BaseClient { this.retryPolicy, this.findProxy, Client? client, - }) : _inner = client ?? Client(); + int? maxActiveRequests, + }) : _inner = client ?? Client(), + _pool = Pool(maxActiveRequests ?? 32); factory InterceptedClient.build({ required List interceptors, @@ -57,6 +61,7 @@ class InterceptedClient extends BaseClient { RetryPolicy? retryPolicy, String Function(Uri)? findProxy, Client? client, + int? maxActiveRequests, }) => InterceptedClient._internal( interceptors: interceptors, @@ -64,6 +69,7 @@ class InterceptedClient extends BaseClient { retryPolicy: retryPolicy, findProxy: findProxy, client: client, + maxActiveRequests: maxActiveRequests, ); @override @@ -183,8 +189,8 @@ class InterceptedClient extends BaseClient { // TODO(codingalecr): Implement interception from `send` method. @override - Future send(BaseRequest request) { - return _inner.send(request); + Future send(BaseRequest request) async { + return await _inner.send(request); } Future _sendUnstreamed({ @@ -195,9 +201,10 @@ class InterceptedClient extends BaseClient { Object? body, Encoding? encoding, }) async { + var resource = await _pool.request(); url = url.addParameters(params); - Request request = new Request(methodToString(method), url); + Request request = Request(methodToString(method), url); if (headers != null) request.headers.addAll(headers); if (encoding != null) request.encoding = encoding; if (body != null) { @@ -208,6 +215,7 @@ class InterceptedClient extends BaseClient { } else if (body is Map) { request.bodyFields = body.cast(); } else { + resource.release(); throw new ArgumentError('Invalid request body "$body".'); } } @@ -217,6 +225,7 @@ class InterceptedClient extends BaseClient { // Intercept response response = await _interceptResponse(response); + resource.release(); return response; } diff --git a/pubspec.lock b/pubspec.lock index 2127182..cf6f9cd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -198,7 +198,7 @@ packages: source: hosted version: "1.11.1" pool: - dependency: transitive + dependency: "direct main" description: name: pool url: "https://pub.dartlang.org" diff --git a/pubspec.yaml b/pubspec.yaml index b9a579a..5b2aa78 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ environment: dependencies: http: ^0.13.3 + pool: ^1.5.0 dev_dependencies: test: ^1.17.5