From 60fd4bcbfd5ec2835244ce8a9976583c0d3495ea Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Wed, 31 Jul 2024 22:53:50 +0800 Subject: [PATCH] Reuse http client instead of making a new one on every connection --- app/lib/cache_manager_util.dart | 6 +++--- app/lib/mobile/download.dart | 2 +- app/lib/web/download.dart | 2 +- np_api/lib/src/api.dart | 2 +- np_http/lib/src/http.dart | 38 +++++++++++++++++++-------------- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/app/lib/cache_manager_util.dart b/app/lib/cache_manager_util.dart index 512cb5a4..de250d23 100644 --- a/app/lib/cache_manager_util.dart +++ b/app/lib/cache_manager_util.dart @@ -42,7 +42,7 @@ class ThumbnailCacheManager { key, stalePeriod: const Duration(days: 30), maxNrOfCacheObjects: 50000, - fileService: HttpFileService(httpClient: makeHttpClient()), + fileService: HttpFileService(httpClient: getHttpClient()), ), ); } @@ -60,7 +60,7 @@ class LargeImageCacheManager { key, stalePeriod: const Duration(days: 30), maxNrOfCacheObjects: 1000, - fileService: HttpFileService(httpClient: makeHttpClient()), + fileService: HttpFileService(httpClient: getHttpClient()), ), ); } @@ -76,7 +76,7 @@ class CoverCacheManager { key, stalePeriod: const Duration(days: 30), maxNrOfCacheObjects: 300, - fileService: HttpFileService(httpClient: makeHttpClient()), + fileService: HttpFileService(httpClient: getHttpClient()), ), ); } diff --git a/app/lib/mobile/download.dart b/app/lib/mobile/download.dart index 515bbcfb..28d23787 100644 --- a/app/lib/mobile/download.dart +++ b/app/lib/mobile/download.dart @@ -66,7 +66,7 @@ class _AndroidDownload extends itf.Download { try { final uri = Uri.parse(url); final req = http.Request("GET", uri)..headers.addAll(headers ?? {}); - final response = await makeHttpClient().send(req); + final response = await getHttpClient().send(req); bool isEnd = false; Object? error; final size = response.contentLength; diff --git a/app/lib/web/download.dart b/app/lib/web/download.dart index 7d708589..01424bb9 100644 --- a/app/lib/web/download.dart +++ b/app/lib/web/download.dart @@ -34,7 +34,7 @@ class _WebDownload extends itf.Download { final uri = Uri.parse(url); final req = http.Request("GET", uri)..headers.addAll(headers ?? {}); final response = - await http.Response.fromStream(await makeHttpClient().send(req)); + await http.Response.fromStream(await getHttpClient().send(req)); if (response.statusCode ~/ 100 != 2) { throw DownloadException( "Failed downloading $filename (HTTP ${response.statusCode})"); diff --git a/np_api/lib/src/api.dart b/np_api/lib/src/api.dart index 160ff4a5..1b2d1d6c 100644 --- a/np_api/lib/src/api.dart +++ b/np_api/lib/src/api.dart @@ -66,7 +66,7 @@ class Api { } _log.finer(req.url); final response = - await http.Response.fromStream(await makeHttpClient().send(req)); + await http.Response.fromStream(await getHttpClient().send(req)); if (!isHttpStatusGood(response.statusCode)) { if (response.statusCode == 404) { _log.severe( diff --git a/np_http/lib/src/http.dart b/np_http/lib/src/http.dart index 530137a3..896b2434 100644 --- a/np_http/lib/src/http.dart +++ b/np_http/lib/src/http.dart @@ -9,38 +9,44 @@ import 'http_stub.dart' if (dart.library.io) 'http_io.dart'; Future initHttp(String appVersion) async { - _userAgent = "nc-photos $appVersion"; + final userAgent = "nc-photos $appVersion"; + Client? client; if (getRawPlatform() == NpPlatform.android) { try { - _cronetEngine = CronetEngine.build( + final cronetEngine = CronetEngine.build( enableHttp2: true, - userAgent: _userAgent, + userAgent: userAgent, ); + client = CronetClient.fromCronetEngine( + cronetEngine, + closeEngine: true, + ); + _log.info("Using cronet backend"); } catch (e, stackTrace) { _log.severe("Failed creating CronetEngine", e, stackTrace); } } else if (getRawPlatform().isApple) { try { - _urlConfig = URLSessionConfiguration.ephemeralSessionConfiguration() - ..httpAdditionalHeaders = {"User-Agent": _userAgent}; + final urlConfig = URLSessionConfiguration.ephemeralSessionConfiguration() + ..httpAdditionalHeaders = {"User-Agent": userAgent}; + client = CupertinoClient.fromSessionConfiguration(urlConfig); + _log.info("Using cupertino backend"); } catch (e, stackTrace) { _log.severe("Failed creating URLSessionConfiguration", e, stackTrace); } } -} - -Client makeHttpClient() { - if (getRawPlatform() == NpPlatform.android && _cronetEngine != null) { - return CronetClient.fromCronetEngine(_cronetEngine!); - } else if (getRawPlatform().isApple && _urlConfig != null) { - return CupertinoClient.fromSessionConfiguration(_urlConfig!); + if (client == null) { + _httpClient = makeHttpClientImpl(userAgent: userAgent); + _log.info("Using dart backend"); } else { - return makeHttpClientImpl(userAgent: _userAgent); + _httpClient = client; } } -late final String _userAgent; -CronetEngine? _cronetEngine; -URLSessionConfiguration? _urlConfig; +Client getHttpClient() { + return _httpClient; +} + +late final Client _httpClient; final _log = Logger("np_http");