RetryConfig class

This class represents an automatic retry configuration.

Retry at Regular Intervals

The simplest way to specify automatic retries is to specify a fixed number of times at fixed intervals. For example, to automatically retry up to 5 times at 3 seconds intervals when a timeout occurs in the communication process with the API, use RetryConfig.ofRegularIntervals like following.

import 'package:twitter_api_v2/twitter_api_v2.dart' as v2;

void main() async {
  final twitter = v2.TwitterApi(
    bearerToken: 'YOUR_TOKEN_HERE',
    retryConfig: v2.RetryConfig.ofRegularIntervals(
      maxAttempts: 5,
      intervalInSeconds: 3,
    ),
    timeout: Duration(seconds: 20),
  );
}

Exponential Backoff Algorithm

But sometimes this retry method is too simplistic: if a timeout occurs when communicating with the API, it is certainly useful to retry multiple times at regular intervals, as in the previous example. However, it is undesirable to have multiple clients sending large numbers of requests to the server at short intervals simultaneously when the server is experiencing a failure.

To solve this problem somewhat, the library supports automatic retries with the Exponential BackOff algorithm. This is an algorithm that exponentially increases the retry interval based on the number of retries performed. It can be enabled with the RetryConfig.ofExponentialBackOff constructor as follows.

import 'package:twitter_api_v2/twitter_api_v2.dart' as v2;

void main() async {
  final twitter = v2.TwitterApi(
    bearerToken: 'YOUR_TOKEN_HERE',
    retryConfig: v2.RetryConfig.ofExponentialBackOff(
      maxAttempts: 5,
    ),
    timeout: Duration(seconds: 20),
  );
}

The Exponential BackOff Algorithm can be enabled by using the RetryConfig.ofExponentialBackOff constructor, as in the example above. And the interval, which increases with the number of retries, is then calculated as follows.

2 ^ retryCount

Exponential Backoff Algorithm and Jitter

However, sometimes simply increasing the wait time exponentially is still not sufficient to distribute retry timing. In addition to simply increasing the interval exponentially, adding a random number called Jitter is effective. This method allows for even greater flexibility in distributing the timing of retries.

RetryConfig.ofExponentialBackOff constructor, as in the example above. As always, you can use this algorithm by using RetryConfig.ofExponentialBackOffAndJitter constructor as follows.

import 'package:twitter_api_v2/twitter_api_v2.dart' as v2;

void main() async {
  final twitter = v2.TwitterApi(
    bearerToken: 'YOUR_TOKEN_HERE',
    retryConfig: v2.RetryConfig.ofExponentialBackOffAndJitter(
      maxAttempts: 5,
    ),
    timeout: Duration(seconds: 20),
  );
}

The Exponential BackOff and Jitter Algorithm can be enabled by using the RetryConfig.ofExponentialBackOffAndJitter constructor, as in the example above. And the interval, which increases with the number of retries, is then calculated as follows.

(2 ^ retryCount) + jitter(Random Number between 0 ~ 3)

Remarks

Please note that ArgumentError is always raised if a negative number is passed to the maxAttempts field of RetryConfig.

Constructors

RetryConfig.ofExponentialBackOff({required int maxAttempts, dynamic onExecute(RetryEvent event)?})
Returns the new instance of RetryConfig of Exponential Back Off.
factory
RetryConfig.ofExponentialBackOffAndJitter({required int maxAttempts, dynamic onExecute(RetryEvent event)?})
Returns the new instance of RetryConfig of Exponential Back Off and Jitter.
factory
RetryConfig.ofRegularIntervals({required int maxAttempts, int intervalInSeconds = 2, dynamic onExecute(RetryEvent event)?})
Returns the new instance of RetryConfig of regular intervals.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
intervalInSeconds int
Interval time in seconds unit.
final
maxAttempts int
Maximum number of retry attempts.
final
onExecute → (dynamic Function(RetryEvent event)?)
A callback function to be called when the retry is executed.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
strategy → RetryStrategy
The strategy of retry.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited