Skip to content

Commit cd58d44

Browse files
feat: Simplify configuration of delay between retries (#122)
* feat: Simplify configuration of delay between retries * fix: duplicate export * docs: Add feature simplifying the configuration of delays between retries to readme file --------- Co-authored-by: Alejandro Ulate <[email protected]>
1 parent 4091111 commit cd58d44

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ http_interceptor: <latest>
5757
- 🍦 Compatible with vanilla Dart projects or Flutter projects.
5858
- 🎉 Null-safety.
5959
- ⏲ Timeout configuration with duration and timeout functions.
60+
- ⏳ Configure the delay for each retry attempt.
6061

6162
## Usage
6263

@@ -236,6 +237,17 @@ class ExpiredTokenRetryPolicy extends RetryPolicy {
236237

237238
You can also set the maximum amount of retry attempts with `maxRetryAttempts` property or override the `shouldAttemptRetryOnException` if you want to retry the request after it failed with an exception.
238239

240+
Sometimes it is helpful to have a cool-down phase between multiple requests. This delay could for example also differ between the first and the second retry attempt as shown in the following example.
241+
242+
```dart
243+
class ExpiredTokenRetryPolicy extends RetryPolicy {
244+
@override
245+
Duration delayRetryAttemptOnResponse({required int retryAttempt}) {
246+
return const Duration(milliseconds: 250) * math.pow(2.0, retryAttempt);
247+
}
248+
}
249+
```
250+
239251
### Using self signed certificates
240252

241253
You can achieve support for self-signed certificates by providing `InterceptedHttp` or `InterceptedClient` with the `client` parameter when using the `build` method on either of those, it should look something like this:

example/lib/weather_app.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22
import 'dart:developer';
33
import 'dart:io';
4+
import 'dart:math' as math;
45

56
import 'package:flutter/material.dart';
67
import 'package:http_interceptor/http_interceptor.dart';
@@ -344,6 +345,11 @@ class ExpiredTokenRetryPolicy extends RetryPolicy {
344345
return false;
345346
}
346347

348+
@override
349+
Duration delayRetryAttemptOnResponse({required int retryAttempt}) {
350+
return const Duration(milliseconds: 250) * math.pow(2.0, retryAttempt);
351+
}
352+
347353
@override
348354
Future<bool> shouldAttemptRetryOnResponse(BaseResponse response) async {
349355
if (response.statusCode == 401) {

lib/http/intercepted_client.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,17 @@ class InterceptedClient extends BaseClient {
285285
retryPolicy!.maxRetryAttempts > _retryCount &&
286286
await retryPolicy!.shouldAttemptRetryOnResponse(response)) {
287287
_retryCount += 1;
288+
await Future.delayed(retryPolicy!
289+
.delayRetryAttemptOnResponse(retryAttempt: _retryCount));
288290
return _attemptRequest(request);
289291
}
290292
} on Exception catch (error) {
291293
if (retryPolicy != null &&
292294
retryPolicy!.maxRetryAttempts > _retryCount &&
293295
await retryPolicy!.shouldAttemptRetryOnException(error, request)) {
294296
_retryCount += 1;
297+
await Future.delayed(retryPolicy!
298+
.delayRetryAttemptOnException(retryAttempt: _retryCount));
295299
return _attemptRequest(request);
296300
} else {
297301
rethrow;

lib/models/retry_policy.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,10 @@ abstract class RetryPolicy {
4747

4848
/// Number of maximum request attempts that can be retried.
4949
final int maxRetryAttempts = 1;
50+
51+
Duration delayRetryAttemptOnException({required int retryAttempt}) =>
52+
Duration.zero;
53+
54+
Duration delayRetryAttemptOnResponse({required int retryAttempt}) =>
55+
Duration.zero;
5056
}

test/models/retry_policy_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ main() {
1414
});
1515
});
1616

17+
group("delayRetryAttemptOnException", () {
18+
test("returns no delay by default", () async {
19+
// Act
20+
final result = testObject.delayRetryAttemptOnException(retryAttempt: 0);
21+
22+
// Assert
23+
expect(result, Duration.zero);
24+
});
25+
});
26+
27+
group("delayRetryAttemptOnResponse", () {
28+
test("returns no delay by default", () async {
29+
// Act
30+
final result = testObject.delayRetryAttemptOnResponse(retryAttempt: 0);
31+
32+
// Assert
33+
expect(result, Duration.zero);
34+
});
35+
});
36+
1737
group("shouldAttemptRetryOnException", () {
1838
test("returns false by default", () async {
1939
expect(

0 commit comments

Comments
 (0)