From 58846732eb9d37cb65f76c93e385d6d6d4dfd596 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Sat, 21 Sep 2024 23:36:25 -0700 Subject: [PATCH] fix: update retry logic in getTwitchBadges function 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). --- lib/models/adapters/chat_state.dart | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/models/adapters/chat_state.dart b/lib/models/adapters/chat_state.dart index 8134a8f7..a28a53f3 100644 --- a/lib/models/adapters/chat_state.dart +++ b/lib/models/adapters/chat_state.dart @@ -100,7 +100,10 @@ class ChatStateAdapter { } Future> 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") @@ -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; }