Skip to content

Commit

Permalink
headConnectionTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangtian616 committed Jan 15, 2025
1 parent ddf5d6b commit 672644f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/src/download/download_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DownloadManager {

late int _isolateCount;

late final Dio _dio = Dio();
late final Dio _dio;

late final int totalBytes;

Expand Down Expand Up @@ -65,9 +65,11 @@ class DownloadManager {
required this.downloadPath,
required this.savePath,
required int isolateCount,
required Duration connectionTimeout,
required Duration receiveTimeout,
}) : _isolateCount = isolateCount {
(_dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient =
() => createProxyHttpClient()..findProxy = ProxyConfig.toFindProxy(proxyConfig);
(_dio.httpClientAdapter as IOHttpClientAdapter).createHttpClient = () => createProxyHttpClient()..findProxy = ProxyConfig.toFindProxy(proxyConfig);
_dio = Dio(BaseOptions(connectTimeout: connectionTimeout, receiveTimeout: receiveTimeout));
}

void tryRecoverFromMetadata(bool deleteWhenUrlMismatch) {
Expand Down Expand Up @@ -205,6 +207,14 @@ class DownloadManager {
}
}

Future<void> changeConnectionTimeout(Duration duration) async {
_dio.options = _dio.options.copyWith(sendTimeout: duration);
}

Future<void> changeReceiveTimeout(Duration duration) async {
_dio.options = _dio.options.copyWith(receiveTimeout: duration);
}

void registerOnProgress(DownloadProgressCallback callback) {
_onProgress = callback;
}
Expand Down Expand Up @@ -246,14 +256,9 @@ class DownloadManager {

try {
response = await retry(
() => _dio.head(
url,
options: Options(sendTimeout: const Duration(seconds: 5), receiveTimeout: const Duration(seconds: 5)),
),
() => _dio.head(url),
maxAttempts: 3,
retryIf: (e) =>
e is DioException &&
(e.type == DioExceptionType.connectionTimeout || e.type == DioExceptionType.sendTimeout || e.type == DioExceptionType.receiveTimeout),
retryIf: (e) => e is DioException && (e.type == DioExceptionType.connectionTimeout || e.type == DioExceptionType.sendTimeout || e.type == DioExceptionType.receiveTimeout),
);
} on DioException catch (e) {
throw JDownloadException(JDownloadExceptionType.fetchContentLengthFailed, error: e);
Expand Down Expand Up @@ -345,7 +350,7 @@ class DownloadManager {
_computeChunkDownloadRange(_chunks, i),
_computeFileWriteOffset(_chunks, i),
);

continue nextIsolate;
}

Expand Down
32 changes: 32 additions & 0 deletions lib/src/task/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class JDownloadTask {

int get isolateCount => _isolateCount;

late Duration _connectionTimeout;

Duration get connectionTimeout => _connectionTimeout;

late Duration _receiveTimeout;

Duration get receiveTimeout => _receiveTimeout;

TaskStatus _status;

TaskStatus get status => _status;
Expand All @@ -31,13 +39,17 @@ class JDownloadTask {
required this.url,
required this.savePath,
required int isolateCount,
Duration headConnectionTimeout = const Duration(seconds: 5),
Duration headReceiveTimeout = const Duration(seconds: 5),
bool deleteWhenUrlMismatch = true,
DownloadProgressCallback? onProgress,
VoidCallback? onDone,
ValueCallback<JDownloadException>? onError,
JDownloadLogCallback? onLog,
ProxyConfig? proxyConfig,
}) : _isolateCount = isolateCount,
_connectionTimeout = headConnectionTimeout,
_receiveTimeout = headReceiveTimeout,
_status = TaskStatus.none {
assert(Uri.tryParse(url) != null, 'Invalid url');
assert(savePath.isNotEmpty, 'Invalid save path');
Expand All @@ -48,6 +60,8 @@ class JDownloadTask {
downloadPath: downloadPath,
savePath: savePath,
isolateCount: isolateCount,
connectionTimeout: headConnectionTimeout,
receiveTimeout: headReceiveTimeout,
)
..registerOnProgress((current, total) {
onProgress?.call(current, total);
Expand Down Expand Up @@ -105,6 +119,24 @@ class JDownloadTask {
_isolateCount = count;
}

Future<void> changeConnectionTimeout(Duration duration) async {
if (_status == TaskStatus.completed) {
return;
}

await _downloadManager.changeConnectionTimeout(duration);
_connectionTimeout = duration;
}

Future<void> changeReceiveTimeout(Duration duration) async {
if (_status == TaskStatus.completed) {
return;
}

await _downloadManager.changeReceiveTimeout(duration);
_receiveTimeout = duration;
}

void setProxy(ProxyConfig? proxyConfig) {
_downloadManager.proxyConfig = proxyConfig;
}
Expand Down

0 comments on commit 672644f

Please sign in to comment.