File tree Expand file tree Collapse file tree 1 file changed +26
-10
lines changed
Expand file tree Collapse file tree 1 file changed +26
-10
lines changed Original file line number Diff line number Diff 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 );
You can’t perform that action at this time.
0 commit comments