feat: retry before sending (#489)

* feat: retry before sending

* reduce idleTimeout
This commit is contained in:
My-Responsitories
2025-03-23 12:09:11 +08:00
committed by GitHub
parent 99b14d0f0e
commit 3881b3dc74
6 changed files with 152 additions and 36 deletions

View File

@@ -4,6 +4,7 @@ import 'dart:developer';
import 'dart:io';
import 'dart:math' show Random;
import 'package:PiliPlus/build_config.dart';
import 'package:PiliPlus/http/retry_interceptor.dart';
import 'package:PiliPlus/utils/accounts/account.dart';
import 'package:PiliPlus/utils/accounts/account_manager/account_mgr.dart';
import 'package:archive/archive.dart';
@@ -96,9 +97,9 @@ class Request {
//请求基地址,可以包含子路径
baseUrl: HttpString.apiBaseUrl,
//连接服务器超时时间,单位是毫秒.
connectTimeout: const Duration(milliseconds: 12000),
connectTimeout: const Duration(milliseconds: 4000),
//响应流上前后两次接受到数据的间隔,单位为毫秒。
receiveTimeout: const Duration(milliseconds: 12000),
receiveTimeout: const Duration(milliseconds: 4000),
//Http请求头.
headers: {
'connection': 'keep-alive',
@@ -121,7 +122,7 @@ class Request {
final http11Adapter = IOHttpClientAdapter(createHttpClient: () {
final client = HttpClient()
..idleTimeout = const Duration(seconds: 30)
..idleTimeout = const Duration(seconds: 15)
..autoUncompress = false; // Http2Adapter没有自动解压, 统一行为
// 设置代理
if (enableSystemProxy) {
@@ -132,24 +133,40 @@ class Request {
return client;
});
late Uri proxy;
if (enableSystemProxy) {
proxy = Uri(
scheme: 'http',
host: systemProxyHost,
port: int.parse(systemProxyPort));
}
dio = Dio(options)
..httpClientAdapter =
GStorage.setting.get(SettingBoxKey.enableHttp2, defaultValue: false)
? Http2Adapter(
ConnectionManager(
idleTimeout: const Duration(seconds: 30),
onClientCreate: (_, ClientSetting config) {
config.onBadCertificate = (_) => true;
if (enableSystemProxy) {
config.proxy = Uri(
scheme: 'http',
host: systemProxyHost,
port: int.parse(systemProxyPort));
}
}),
idleTimeout: const Duration(seconds: 15),
onClientCreate: enableSystemProxy
? (_, config) {
config
..proxy = proxy
..onBadCertificate = (_) => true;
}
: GStorage.badCertificateCallback
? (_, config) {
config.onBadCertificate = (_) => true;
}
: null),
fallbackAdapter: http11Adapter)
: http11Adapter;
// 先于其他Interceptor
if (GStorage.retryCount > 0) {
dio.interceptors
.add(RetryInterceptor(GStorage.retryCount, GStorage.retryDelay));
}
// 日志拦截器 输出请求、响应内容
if (BuildConfig.isDebug) {
dio.interceptors.add(LogInterceptor(

View File

@@ -0,0 +1,34 @@
import 'package:dio/dio.dart';
import 'index.dart';
class RetryInterceptor extends Interceptor {
final int _count;
final int _delay;
RetryInterceptor(this._count, this._delay);
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
if (err.response != null) return handler.next(err);
switch (err.type) {
case DioExceptionType.connectionError:
case DioExceptionType.connectionTimeout:
case DioExceptionType.sendTimeout:
if ((err.requestOptions.extra['_rt'] ??= 0) < _count) {
Future.delayed(
Duration(
milliseconds: ++err.requestOptions.extra['_rt'] * _delay),
() => Request.dio
.fetch(err.requestOptions)
.then(handler.resolve)
.onError<DioException>((error, _) => handler.reject(error)));
} else {
handler.next(err);
}
return;
default:
return handler.next(err);
}
}
}