mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-02 06:46:22 +01:00
Reuse http client instead of making a new one on every connection
This commit is contained in:
parent
eb752e57a2
commit
60fd4bcbfd
5 changed files with 28 additions and 22 deletions
|
@ -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()),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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})");
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -9,38 +9,44 @@ import 'http_stub.dart'
|
|||
if (dart.library.io) 'http_io.dart';
|
||||
|
||||
Future<void> 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");
|
||||
|
|
Loading…
Reference in a new issue