パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v7.0.1

dio_smart_retry

DioとDioパッケージ用のリトライライブラリ、愛を込めて作成。デフォルトでは、適切な再試行可能なHTTPステータスに対してのみリクエストが再試行されます。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
20.9万
いいね数
314
Pub ポイント
145

パッケージをインストール

flutter pub add dio_smart_retry

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Dio Smart Retry は、Dio HTTP クライアント向けのリトライユーティリティで、事前に定義された条件に基づいてリクエストを自動的に再試行するように設計されています。デフォルトでは、5xx サーバーエラーまたはネットワークタイムアウトなど、リトライ可能な HTTP ステータスコードに対してのみリトライを行います。シンプルさと信頼性を重視して開発されており、既存の Dio 設定にスムーズに統合できます。このパッケージは Android、iOS、Web、macOS、Windows、Linux といったすべての主要プラットフォームをサポートしており、クロスプラットフォームな Flutter アプリにおける堅牢なネットワーク操作に最適です。

用途

  • 一時的なネットワーク障害の処理
  • サーバーエラーに対する再試行の自動化
  • 接続状態が悪いときのアプリの回復力向上
  • API 呼び出しにおけるエラー処理の簡素化
  • ビジネスコード内の手動リトライロジックの削減

主な機能

  • 再試可能HTTPステータスの自動リトライ
  • Dioとの簡単な統合
  • 複数のプラットフォームをサポートしています
  • 設定可能な再試行動作
  • 軽量でオーバーヘッドが最小限

適している対象

  • 頻繁なAPI呼び出しを持つFlutterアプリ
  • 信頼性のあるネットワーク通信を必要とするアプリ
  • シンプルな再試行ロジックを求める開発者
  • HTTPリクエストにDioを使用しているプロジェクト
  • ボイラープレートエラー処理を減らしたいチーム

注意事項

  • デフォルトでは、クライアントエラー(4xx)に対して再試行しません
  • カスタム再試行ロジックには追加の構成が必要です
  • DioベースのHTTPクライアントに限定
  • 高遅延環境向けに調整が必要になる可能性があります
  • 基本的な再試行を超える組み込みのバックオフ戦略はありません

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Pub Version Dart SDK Version style: very good analysis License

Dio Smart Retry

Flexible retry library for Dio package. This is a next generation of an abandoned dio_retry package.
By default, the request will be retried only for appropriate retryable http statuses.
Also, it supports dynamic delay between retries.
Null Safety.

Contents

Getting Started for Dio

  1. Add package to pubspec.yaml: dio_smart_retry: ^7.0.0 **
  2. Import package: import 'package:dio_smart_retry/dio_smart_retry.dart'

** For the old dio (ver. 4.+) use dio_smart_retry: ^1.4.0

Usage

Just add an interceptor to your dio:

final dio = Dio();
// Add the interceptor
dio.interceptors.add(RetryInterceptor(
  dio: dio,
  logPrint: print, // specify log function (optional)
  retries: 3, // retry count (optional)
  retryDelays: const [ // set delays between retries (optional)
    Duration(seconds: 1), // wait 1 sec before first retry
    Duration(seconds: 2), // wait 2 sec before second retry
    Duration(seconds: 3), // wait 3 sec before third retry
  ],
));

/// Sending a failing request for 3 times with 1s, then 2s, then 3s interval
await dio.get('https://mock.codes/500');

See example/dio_smart_retry_example.dart.

Default retryable status codes list

Responses with these http status codes will be retried by default:

  • 408: RequestTimeout
  • 429: TooManyRequests
  • 500: InternalServerError
  • 502: BadGateway
  • 503: ServiceUnavailable
  • 504: GatewayTimeout
  • 440: LoginTimeout (IIS)
  • 460: ClientClosedRequest (AWS Elastic Load Balancer)
  • 499: ClientClosedRequest (ngnix)
  • 520: WebServerReturnedUnknownError
  • 521: WebServerIsDown
  • 522: ConnectionTimedOut
  • 523: OriginIsUnreachable
  • 524: TimeoutOccurred
  • 525: SSLHandshakeFailed
  • 527: RailgunError
  • 598: NetworkReadTimeoutError
  • 599: NetworkConnectTimeoutError It's possible to override this list

Disable retry

It's possible to manually disable retry for a specified request. Use disableRetry extension for that:

final request = RequestOptions(path: '/')
  ..disableRetry = true;
await dio.fetch<String>(request);

or

final options = Options()
  ..disableRetry = true;
await dio.get<String>('/', options: options);

Add extra retryable status codes

It's possible to add you own retryable status codes. Use retryableExtraStatuses parameter for that. Here is an example:

RetryInterceptor(
  dio: dio,
  retryableExtraStatuses: { status401Unauthorized },
)

or:

RetryInterceptor(
  dio: dio,
  retryableExtraStatuses: { 401 },
)

Override retryable statuses

It's possible to override default retryable status codes list. Just make new instance of DefaultRetryEvaluator and pass your status codes there.
Here is an example:

final myStatuses = { status400BadRequest, status409Conflict };
dio.interceptors.add(
  RetryInterceptor(
    dio: dio,
    logPrint: print,
    retryEvaluator: DefaultRetryEvaluator(myStatuses).evaluate,
  ),
);

await dio.get<dynamic>('https://mock.codes/400');

Migrating to 7.0

Use MultipartFile instead of MultipartFileRecreatable.

カテゴリー

依存関係

依存関係一覧 3 件

ソースリポジトリ

このパッケージを使用しているプロジェクト