Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update retry logic in getTwitchBadges function #1324

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/models/adapters/chat_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ class ChatStateAdapter {
}

Future<List<TwitchBadgeInfo>> getTwitchBadges({String? channelId}) async {
for (int attempt = 0; attempt < 3; attempt++) {
const int maxAttempts = 10;
const int baseDelay = 1; // in seconds

for (int attempt = 0; attempt < maxAttempts; attempt++) {
try {
final result = await functions
.httpsCallable("getBadges")
Expand All @@ -126,10 +129,13 @@ class ChatStateAdapter {
))
.toList();
} catch (e) {
if (e is FirebaseFunctionsException &&
e.code == 'unavailable' &&
attempt < 2) {
await Future.delayed(const Duration(seconds: 1));
if (e is FirebaseFunctionsException && e.code == 'unavailable') {
if (attempt < maxAttempts - 1) {
await Future.delayed(Duration(seconds: baseDelay * (1 << attempt)));
} else {
// Handle the "UNAVAILABLE" error gracefully beyond the 10 attempts
return [];
}
} else {
rethrow;
}
Expand Down
Loading