Skip to content

Commit 47046a4

Browse files
committed
Create Stat Tracker Network Retry
1 parent 760661f commit 47046a4

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

lib/src/logging/stat_tracker.dart

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,34 @@ class StatTracker {
5757
}
5858
}
5959

60-
/// Sends the batch of stats to the server.
60+
/// Sends the batch of stats to the server with retry logic.
6161
Future<void> sendBatch() => debouncer.run(
6262
() async {
63-
unawaited(client.post(
64-
serverUrl!,
65-
headers: <String, String>{'Content-Type': 'application/json'},
66-
body: jsonEncode({
67-
'projectId': projectId,
68-
'stats': statBatch,
69-
}),
70-
));
71-
statBatch.clear();
63+
int maxRetries = 3;
64+
int baseDelaySeconds = 2;
65+
66+
for (int attempt = 0; attempt < maxRetries; attempt++) {
67+
try {
68+
final response = await client.post(
69+
serverUrl!,
70+
headers: <String, String>{'Content-Type': 'application/json'},
71+
body: jsonEncode({
72+
'projectId': projectId,
73+
'stats': statBatch,
74+
}),
75+
);
76+
77+
if (response.statusCode == 200) {
78+
statBatch.clear();
79+
return; // Exit the function if successful
80+
}
81+
} catch (e) {
82+
// Handle exception silently
83+
}
84+
await Future.delayed(Duration(
85+
seconds:
86+
baseDelaySeconds * (1 << attempt))); // Exponential backoff
87+
}
7288
},
7389
forceRunAfter: 20,
7490
);

0 commit comments

Comments
 (0)