Skip to content

Commit

Permalink
fix: update retry logic in getTwitchBadges function
Browse files Browse the repository at this point in the history
Update `getTwitchBadges` function to retry up to 10 times with exponential back off.

* Modify the `getTwitchBadges` function in `lib/models/adapters/chat_state.dart` to retry up to 10 times.
* Implement exponential back off with a base delay of 1 second.
* Increase the delay exponentially with each retry.
* Handle the "UNAVAILABLE" error gracefully beyond the 10 attempts.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/muxable/rtchat?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
kevmo314 committed Sep 22, 2024
1 parent 55faf8a commit 5884673
Showing 1 changed file with 11 additions and 5 deletions.
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

0 comments on commit 5884673

Please sign in to comment.