Skip to content

Commit

Permalink
🐛 Fix too many request error
Browse files Browse the repository at this point in the history
  • Loading branch information
redDwarf03 committed Jun 17, 2024
1 parent d50d1c2 commit 49a027d
Showing 1 changed file with 65 additions and 13 deletions.
78 changes: 65 additions & 13 deletions lib/service/app_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class AppService {
await Future.delayed(const Duration(seconds: 1));
}

// Make the API call and update the antiSpam counter
futures.add(
sl.get<ApiService>().getToken(
[address],
_retryOnException(
() => sl.get<ApiService>().getToken([address]),
actionDescription: 'address: $address',
),
);
antiSpam++;
Expand Down Expand Up @@ -102,13 +102,17 @@ class AppService {

// Make the API call and update the antiSpam counter
futures.add(
sl.get<ApiService>().getTransactionInputs(
[address],
request: request,
limit: limit,
pagingOffset: pagingOffset,
_retryOnException(
() => sl.get<ApiService>().getTransactionInputs(
[address],
request: request,
limit: limit,
pagingOffset: pagingOffset,
),
actionDescription: 'address: $address',
),
);

antiSpam++;
}

Expand Down Expand Up @@ -513,10 +517,14 @@ class AppService {

// Make the API call and update the antiSpam counter
futures.add(
sl.get<ApiService>().getTransactionOwnerships(
[ownershipsAddress],
_retryOnException(
() => sl.get<ApiService>().getTransactionOwnerships(
[ownershipsAddress],
),
actionDescription: 'ownershipsAddress: $ownershipsAddress',
),
);

antiSpam++;
}

Expand Down Expand Up @@ -577,11 +585,16 @@ class AppService {

// Make the API call and update the antiSpam counter
futures.add(
sl.get<ApiService>().getLastTransaction(
[lastTransactionAddressToSearch],
request: 'address',
_retryOnException(
() => sl.get<ApiService>().getLastTransaction(
[lastTransactionAddressToSearch],
request: 'address',
),
actionDescription:
'lastTransactionAddressToSearch: $lastTransactionAddressToSearch',
),
);

antiSpam++;
}

Expand Down Expand Up @@ -1142,4 +1155,43 @@ class AppService {
}
return fromBigInt(transactionFee.fee).toDouble();
}

/// Retry function with a delay for Too Many Requests errors.
Future<T> _retryOnException<T>(
Future<T> Function() action, {
String? actionDescription,
}) async {
const maxRetries = 3;
const delaySeconds = 5;

var retryCount = 0;
while (retryCount < maxRetries) {
try {
_logger.info('${DateTime.now()} Call $action');
if (actionDescription != null) {
_logger.info(
'${DateTime.now()} $actionDescription}',
);
}
return await action();
} catch (e) {
if (e is ArchethicTooManyRequestsException) {
retryCount++;
_logger.info('${DateTime.now()} Retry $action}');
if (actionDescription != null) {
_logger.info(
'${DateTime.now()} $actionDescription}',
);
}

await Future.delayed(const Duration(seconds: delaySeconds));
} else {
rethrow;
}
}
}

print('${DateTime.now()} Max retries exceeded $action');
throw Exception('Max retries exceeded');
}
}

0 comments on commit 49a027d

Please sign in to comment.