Reuse http client instead of making a new one on every connection

This commit is contained in:
Ming Ming 2024-07-31 22:53:50 +08:00
parent eb752e57a2
commit 60fd4bcbfd
5 changed files with 28 additions and 22 deletions

View file

@ -42,7 +42,7 @@ class ThumbnailCacheManager {
key, key,
stalePeriod: const Duration(days: 30), stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 50000, maxNrOfCacheObjects: 50000,
fileService: HttpFileService(httpClient: makeHttpClient()), fileService: HttpFileService(httpClient: getHttpClient()),
), ),
); );
} }
@ -60,7 +60,7 @@ class LargeImageCacheManager {
key, key,
stalePeriod: const Duration(days: 30), stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 1000, maxNrOfCacheObjects: 1000,
fileService: HttpFileService(httpClient: makeHttpClient()), fileService: HttpFileService(httpClient: getHttpClient()),
), ),
); );
} }
@ -76,7 +76,7 @@ class CoverCacheManager {
key, key,
stalePeriod: const Duration(days: 30), stalePeriod: const Duration(days: 30),
maxNrOfCacheObjects: 300, maxNrOfCacheObjects: 300,
fileService: HttpFileService(httpClient: makeHttpClient()), fileService: HttpFileService(httpClient: getHttpClient()),
), ),
); );
} }

View file

@ -66,7 +66,7 @@ class _AndroidDownload extends itf.Download {
try { try {
final uri = Uri.parse(url); final uri = Uri.parse(url);
final req = http.Request("GET", uri)..headers.addAll(headers ?? {}); final req = http.Request("GET", uri)..headers.addAll(headers ?? {});
final response = await makeHttpClient().send(req); final response = await getHttpClient().send(req);
bool isEnd = false; bool isEnd = false;
Object? error; Object? error;
final size = response.contentLength; final size = response.contentLength;

View file

@ -34,7 +34,7 @@ class _WebDownload extends itf.Download {
final uri = Uri.parse(url); final uri = Uri.parse(url);
final req = http.Request("GET", uri)..headers.addAll(headers ?? {}); final req = http.Request("GET", uri)..headers.addAll(headers ?? {});
final response = final response =
await http.Response.fromStream(await makeHttpClient().send(req)); await http.Response.fromStream(await getHttpClient().send(req));
if (response.statusCode ~/ 100 != 2) { if (response.statusCode ~/ 100 != 2) {
throw DownloadException( throw DownloadException(
"Failed downloading $filename (HTTP ${response.statusCode})"); "Failed downloading $filename (HTTP ${response.statusCode})");

View file

@ -66,7 +66,7 @@ class Api {
} }
_log.finer(req.url); _log.finer(req.url);
final response = final response =
await http.Response.fromStream(await makeHttpClient().send(req)); await http.Response.fromStream(await getHttpClient().send(req));
if (!isHttpStatusGood(response.statusCode)) { if (!isHttpStatusGood(response.statusCode)) {
if (response.statusCode == 404) { if (response.statusCode == 404) {
_log.severe( _log.severe(

View file

@ -9,38 +9,44 @@ import 'http_stub.dart'
if (dart.library.io) 'http_io.dart'; if (dart.library.io) 'http_io.dart';
Future<void> initHttp(String appVersion) async { Future<void> initHttp(String appVersion) async {
_userAgent = "nc-photos $appVersion"; final userAgent = "nc-photos $appVersion";
Client? client;
if (getRawPlatform() == NpPlatform.android) { if (getRawPlatform() == NpPlatform.android) {
try { try {
_cronetEngine = CronetEngine.build( final cronetEngine = CronetEngine.build(
enableHttp2: true, enableHttp2: true,
userAgent: _userAgent, userAgent: userAgent,
); );
client = CronetClient.fromCronetEngine(
cronetEngine,
closeEngine: true,
);
_log.info("Using cronet backend");
} catch (e, stackTrace) { } catch (e, stackTrace) {
_log.severe("Failed creating CronetEngine", e, stackTrace); _log.severe("Failed creating CronetEngine", e, stackTrace);
} }
} else if (getRawPlatform().isApple) { } else if (getRawPlatform().isApple) {
try { try {
_urlConfig = URLSessionConfiguration.ephemeralSessionConfiguration() final urlConfig = URLSessionConfiguration.ephemeralSessionConfiguration()
..httpAdditionalHeaders = {"User-Agent": _userAgent}; ..httpAdditionalHeaders = {"User-Agent": userAgent};
client = CupertinoClient.fromSessionConfiguration(urlConfig);
_log.info("Using cupertino backend");
} catch (e, stackTrace) { } catch (e, stackTrace) {
_log.severe("Failed creating URLSessionConfiguration", e, stackTrace); _log.severe("Failed creating URLSessionConfiguration", e, stackTrace);
} }
} }
} if (client == null) {
_httpClient = makeHttpClientImpl(userAgent: userAgent);
Client makeHttpClient() { _log.info("Using dart backend");
if (getRawPlatform() == NpPlatform.android && _cronetEngine != null) {
return CronetClient.fromCronetEngine(_cronetEngine!);
} else if (getRawPlatform().isApple && _urlConfig != null) {
return CupertinoClient.fromSessionConfiguration(_urlConfig!);
} else { } else {
return makeHttpClientImpl(userAgent: _userAgent); _httpClient = client;
} }
} }
late final String _userAgent; Client getHttpClient() {
CronetEngine? _cronetEngine; return _httpClient;
URLSessionConfiguration? _urlConfig; }
late final Client _httpClient;
final _log = Logger("np_http"); final _log = Logger("np_http");