Skip to content

Commit

Permalink
Implement exponential backoff retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Dec 22, 2024
1 parent 5074216 commit fcd08fc
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/resident_manager/lib/src/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ class ApplicationState {
Map<String, String>? queryParameters,
Map<String, String>? headers,
bool authorize = true,
int retry = 3,
int retry = 5,
Duration retryWait = const Duration(milliseconds: 200),
}) async {
headers ??= {};
if (authorize) {
Expand All @@ -192,12 +193,14 @@ class ApplicationState {
);

if (retry > 0 && response.statusCode >= 400 && await _reauthorize(response)) {
await Future.delayed(retryWait);
return await get(
path,
queryParameters: queryParameters,
headers: headers,
authorize: authorize,
retry: retry - 1,
retryWait: retryWait * 2,
);
}

Expand All @@ -211,7 +214,8 @@ class ApplicationState {
Object? body,
Encoding? encoding,
bool authorize = true,
int retry = 3,
int retry = 5,
Duration retryWait = const Duration(milliseconds: 200),
}) async {
headers ??= {};
if (authorize) {
Expand All @@ -226,6 +230,7 @@ class ApplicationState {
);

if (retry > 0 && response.statusCode >= 400 && await _reauthorize(response)) {
await Future.delayed(retryWait);
return await post(
path,
queryParameters: queryParameters,
Expand All @@ -234,6 +239,7 @@ class ApplicationState {
encoding: encoding,
authorize: authorize,
retry: retry - 1,
retryWait: retryWait * 2,
);
}

Expand Down

0 comments on commit fcd08fc

Please sign in to comment.