From 34f1a4d98573dce7d995540257d0b08565ee4a7b Mon Sep 17 00:00:00 2001 From: Chris Chua Date: Wed, 13 Mar 2024 11:55:24 +0800 Subject: [PATCH 1/2] backoff: add reset method to reset to minimal wait --- lib/api/backoff.dart | 6 ++++++ test/api/backoff_test.dart | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/api/backoff.dart b/lib/api/backoff.dart index e895d06ef9..aee866de50 100644 --- a/lib/api/backoff.dart +++ b/lib/api/backoff.dart @@ -49,4 +49,10 @@ class BackoffMachine { _waitsCompleted++; } + + /// Reset the machine to its initial state with minimal wait. + void reset() { + _startTime = null; + _waitsCompleted = 0; + } } diff --git a/test/api/backoff_test.dart b/test/api/backoff_test.dart index 132dcf0eee..19144e045e 100644 --- a/test/api/backoff_test.dart +++ b/test/api/backoff_test.dart @@ -51,4 +51,17 @@ void main() { check(maxFromAllTrials).isGreaterThan(expectedMax * 0.75); } }); + + test('BackoffMachine resets duration', () async { + final backoffMachine = BackoffMachine(); + await measureWait(backoffMachine.wait()); + final duration2 = await measureWait(backoffMachine.wait()); + check(backoffMachine.waitsCompleted).equals(2); + + backoffMachine.reset(); + check(backoffMachine.waitsCompleted).equals(0); + final durationReset1 = await measureWait(backoffMachine.wait()); + check(durationReset1).isLessThan(duration2); + check(backoffMachine.waitsCompleted).equals(1); + }); } From 246b7b3c800dffba6de72e61bc4ef43a61b50028 Mon Sep 17 00:00:00 2001 From: Chris Chua Date: Wed, 13 Mar 2024 12:20:11 +0800 Subject: [PATCH 2/2] store: reset backoff on success Fixes: #554 --- lib/model/store.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/model/store.dart b/lib/model/store.dart index ce2400200d..f3e2521669 100644 --- a/lib/model/store.dart +++ b/lib/model/store.dart @@ -694,7 +694,6 @@ class UpdateMachine { assert(debugLog('Transient error polling event queue for $store: $e\n' 'Backing off, then will retry…')); // TODO tell user if transient polling errors persist - // TODO reset to short backoff eventually await backoffMachine.wait(); assert(debugLog('… Backoff wait complete, retrying poll.')); continue; @@ -716,6 +715,9 @@ class UpdateMachine { if (events.isNotEmpty) { lastEventId = events.last.id; } + + // On success, reset the backoff. + backoffMachine.reset(); } }