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

@@ -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);
}
}
}