Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a pool for connections #93

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/http/intercepted_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -42,28 +43,33 @@ class InterceptedClient extends BaseClient {

int _retryCount = 0;
late Client _inner;
late Pool _pool;

InterceptedClient._internal({
required this.interceptors,
this.requestTimeout,
this.retryPolicy,
this.findProxy,
Client? client,
}) : _inner = client ?? Client();
int? maxActiveRequests,
}) : _inner = client ?? Client(),
_pool = Pool(maxActiveRequests ?? 32);

factory InterceptedClient.build({
required List<InterceptorContract> interceptors,
Duration? requestTimeout,
RetryPolicy? retryPolicy,
String Function(Uri)? findProxy,
Client? client,
int? maxActiveRequests,
}) =>
InterceptedClient._internal(
interceptors: interceptors,
requestTimeout: requestTimeout,
retryPolicy: retryPolicy,
findProxy: findProxy,
client: client,
maxActiveRequests: maxActiveRequests,
);

@override
Expand Down Expand Up @@ -183,8 +189,8 @@ class InterceptedClient extends BaseClient {

// TODO(codingalecr): Implement interception from `send` method.
@override
Future<StreamedResponse> send(BaseRequest request) {
return _inner.send(request);
Future<StreamedResponse> send(BaseRequest request) async {
return await _inner.send(request);
}

Future<Response> _sendUnstreamed({
Expand All @@ -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) {
Expand All @@ -208,6 +215,7 @@ class InterceptedClient extends BaseClient {
} else if (body is Map) {
request.bodyFields = body.cast<String, String>();
} else {
resource.release();
throw new ArgumentError('Invalid request body "$body".');
}
}
Expand All @@ -217,6 +225,7 @@ class InterceptedClient extends BaseClient {
// Intercept response
response = await _interceptResponse(response);

resource.release();
return response;
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:

dependencies:
http: ^0.13.3
pool: ^1.5.0

dev_dependencies:
test: ^1.17.5
Expand Down