Giter Site home page Giter Site logo

teamzira / dio_smart_retry Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rodion-m/dio_smart_retry

0.0 0.0 0.0 43 KB

Flexible retry library for Dio package. A next generation of an abandoned dio_retry package.

License: MIT License

Dart 100.00%

dio_smart_retry's Introduction

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.

Getting started

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

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

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 },
)

dio_smart_retry's People

Contributors

rodion-m avatar nin-echo avatar thesadboy avatar barteksc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.