mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
feat: retry before sending (#489)
* feat: retry before sending * reduce idleTimeout
This commit is contained in:
committed by
GitHub
parent
99b14d0f0e
commit
3881b3dc74
34
lib/http/retry_interceptor.dart
Normal file
34
lib/http/retry_interceptor.dart
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user