8000 feat: retry before sending by My-Responsitories · Pull Request #489 · bggRGjQaUbCoE/PiliPlus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: retry before sending #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions lib/http/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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) {
Expand All @@ -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. => 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
.. => true;
}
: GStorage.badCertificateCallback
? (_, config) {
config. => 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(
Expand Down
34 changes: 34 additions & 0 deletions lib/http/retry_interceptor.dart
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class _CustomHttpOverrides extends HttpOverrides {
HttpClient createHttpClient(SecurityContext? context) {
final client = super.createHttpClient(context)
..maxConnectionsPerHost = 32
..idleTimeout = const Duration(seconds: 30);
..idleTimeout = const Duration(seconds: 15);
if (badCertificateCallback) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
Expand Down
80 changes: 68 additions & 12 deletions lib/pages/settin 8000 g/widgets/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ List<SettingsModel> get styleSettings => [
double? result = await showDialog(
context: Get.context!,
builder: (context) {
return SlideDialog<double>(
return SlideDialog(
title: '小卡最大列宽度(默认240dp)',
value: GStorage.smallCardWidth,
min: 150.0,
Expand All @@ -186,7 +186,7 @@ List<SettingsModel> get styleSettings => [
double? result = await showDialog(
context: Get.context!,
builder: (context) {
return SlideDialog<double>(
return SlideDialog(
title: '中卡最大列宽度(默认280dp)',
value: GStorage.mediumCardWidth,
min: 150.0,
Expand Down Expand Up @@ -449,7 +449,7 @@ List<SettingsModel> get styleSettings => [
double? result = await showDialog(
context: Get.context!,
builder: (context) {
return SlideDialog<double>(
return SlideDialog(
title: 'Toast不透明度',
value: GStorage.toastOpacity,
min: 0.0,
Expand Down Expand Up @@ -691,7 +691,7 @@ void _showQualityDialog({
),
actions: [
TextButton(
onPressed: () => Get.back(),
onPressed: Get.back,
child: Text('取消',
style:
TextStyle(color: Theme.of(context).colorScheme.outline))),
Expand Down Expand Up @@ -1795,7 +1795,7 @@ List<SettingsModel> get extraSettings => [
double? result = await showDialog(
context: Get.context!,
builder: (context) {
return SlideDialog<double>(
return SlideDialog(
title: '刷新滑动距离',
min: 0.1,
max: 0.5,
Expand Down Expand Up @@ -1825,7 +1825,7 @@ List<SettingsModel> get extraSettings => [
double? result = await showDialog(
context: Get.context!,
builder: (context) {
return SlideDialog<double>(
return SlideDialog(
title: '刷新指示器高度',
min: 10.0,
max: 100.0,
Expand Down Expand Up @@ -2214,12 +2214,68 @@ List<SettingsModel> get extraSettings => [
defaultVal: false,
),
SettingsModel(
settingsType: SettingsType.sw1tch,
title: '启用HTTP/2',
subtitle: '测试中',
leading: Icon(Icons.swap_horizontal_circle_outlined),
setKey: SettingBoxKey.enableHttp2,
defaultVal: false,
settingsType: SettingsType.sw1tch,
title: '启用HTTP/2',
leading: const Icon(Icons.swap_horizontal_circle_outlined),
setKey: SettingBoxKey.enableHttp2,
defaultVal: false,
onChanged: (_) {
SmartDialog.showToast('重启生效');
}),
SettingsModel(
settingsType: SettingsType.normal,
title: '连接重试次数',
subtitle: '为0时禁用',
leading: const Icon(Icons.repeat),
onTap: (setState) async {
final result = await showDialog<double>(
context: Get.context!,
builder: (context) {
return SlideDialog(
title: '连接重试次数',
min: 0,
max: 8,
divisions: 8,
precise: 0,
value: GStorage.retryCount.toDouble(),
);
},
);
if (result != null) {
await GStorage.setting
.put(SettingBoxKey.retryCount, result.toInt());
setState();
SmartDialog.showToast('重启生效');
}
},
),
SettingsModel(
settingsType: SettingsType.normal,
title: '连接重试间隔',
subtitle: '实际间隔 = 间隔 * 第x次重试',
leading: const Icon(Icons.more_time_outlined),
onTap: (setState) async {
final result = await showDialog<double>(
context: Get.context!,
builder: (context) {
return SlideDialog(
title: '连接重试间隔',
min: 0,
max: 1000,
divisions: 10,
precise: 0,
value: GStorage.retryDelay.toDouble(),
suffix: 'ms',
);
},
);
if (result != null) {
await GStorage.setting
.put(SettingBoxKey.retryDelay, result.toInt());
setState();
SmartDialog.showToast('重启生效');
}
},
),
SettingsModel(
settingsType: SettingsType.normal,
Expand Down
21 changes: 11 additions & 10 deletions lib/pages/setting/widgets/slide_dialog.dart
F438
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:flutter/material.dart';
import 'package:get/get_utils/get_utils.dart';

class SlideDialog<T extends num> extends StatefulWidget {
final T value;
class SlideDialog extends StatefulWidget {
final double value;
final String title;
final double min;
final double max;
final int? divisions;
final String? suffix;
final String suffix;
final int precise;

const SlideDialog({
Expand All @@ -17,21 +17,21 @@ class SlideDialog<T extends num> extends StatefulWidget {
required this.min,
required this.max,
this.divisions,
this.suffix,
this.suffix = '',
this.precise = 1,
});

@override
State<SlideDialog<T>> createState() => _SlideDialogState<T>();
State<SlideDialog> createState() => _SlideDialogState();
}

class _SlideDialogState<T extends num> extends State<SlideDialog<T>> {
class _SlideDialogState extends State<SlideDialog> {
late double _tempValue;

@override
void initState() {
super.initState();
_tempValue = widget.value.toDouble();
_tempValue = widget.value;
}

@override
Expand All @@ -47,7 +47,8 @@ class _SlideDialogState<T extends num> extends State<SlideDialog<T>> {
min: widget.min,
max: widget.max,
divisions: widget.divisions,
label: '$_tempValue${widget.suffix ?? ''}',
label:
'${_tempValue.toStringAsFixed(widget.precise)}${widget.suffix}',
onChanged: (double value) {
setState(() {
_tempValue = value.toPrecision(widget.precise);
Expand All @@ -57,14 +58,14 @@ class _SlideDialogState<T extends num> extends State<SlideDialog<T>> {
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
onPressed: Navigator.of(context).pop,
child: Text(
'取消',
style: TextStyle(color: Theme.of(context).colorScheme.outline),
),
),
TextButton(
onPressed: () => Navigator.pop(context, _tempValue as T),
onPressed: () => Navigator.pop(context, _tempValue),
child: const Text('确定'),
)
],
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,12 @@ class GStorage {
static bool get enableSlideVolumeBrightness => GStorage.setting
.get(SettingBoxKey.enableSlideVolumeBrightness, defaultValue: true);

static int get retryCount =>
GStorage.setting.get(SettingBoxKey.retryCount, defaultValue: 0);

static int get retryDelay =>
GStorage.setting.get(SettingBoxKey.retryDelay, defaultValue: 500);

static List<double> get dynamicDetailRatio => List<double>.from(setting
.get(SettingBoxKey.dynamicDetailRatio, defaultValue: [60.0, 40.0]));

Expand Down Expand Up @@ -688,6 +694,8 @@ class SettingBoxKey {
showDynActionBar = 'showDynActionBar',
darkVideoPage = 'darkVideoPage',
enableSlideVolumeBrightness = 'enableSlideVolumeBrightness',
retryCount = 'retryCount',
retryDelay = 'retryDelay',

// Sponsor Block
enableSponsorBlock = 'enableSponsorBlock',
Expand Down
0