Skip to content

Commit 331f65c

Browse files
authored
fix: update retry logic in getTwitchBadges function (#1324)
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).
1 parent 55faf8a commit 331f65c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/models/adapters/chat_state.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ class ChatStateAdapter {
100100
}
101101

102102
Future<List<TwitchBadgeInfo>> getTwitchBadges({String? channelId}) async {
103-
for (int attempt = 0; attempt < 3; attempt++) {
103+
const int maxAttempts = 10;
104+
const int baseDelay = 1; // in seconds
105+
106+
for (int attempt = 0; attempt < maxAttempts; attempt++) {
104107
try {
105108
final result = await functions
106109
.httpsCallable("getBadges")
@@ -126,10 +129,13 @@ class ChatStateAdapter {
126129
))
127130
.toList();
128131
} catch (e) {
129-
if (e is FirebaseFunctionsException &&
130-
e.code == 'unavailable' &&
131-
attempt < 2) {
132-
await Future.delayed(const Duration(seconds: 1));
132+
if (e is FirebaseFunctionsException && e.code == 'unavailable') {
133+
if (attempt < maxAttempts - 1) {
134+
await Future.delayed(Duration(seconds: baseDelay * (1 << attempt)));
135+
} else {
136+
// Handle the "UNAVAILABLE" error gracefully beyond the 10 attempts
137+
return [];
138+
}
133139
} else {
134140
rethrow;
135141
}

0 commit comments

Comments
 (0)