Skip to content

Commit 6c9f66d

Browse files
committed
fix: Update exponential backoff logic for network errors
fixes #2141 Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 0eaf4c4 commit 6c9f66d

4 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/lib/Account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export default class Account {
188188
Logger.log('Starting sync process for account ' + this.getLabel())
189189
setUser({ id: this.id })
190190
this.syncing = true
191-
await this.setData({ syncing: 0.05, scheduled: false, error: null })
191+
await this.setData({ syncing: 0.05, scheduled: false, error: null, lastAttempt: Date.now() })
192192

193193
if (!(await this.isInitialized())) {
194194
await this.init()

src/lib/browser/BrowserController.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class AlarmManager {
3232
const account = await Account.get(accountId)
3333
const data = account.getData()
3434
const lastSync = data.lastSync || 0
35+
const lastAttempt = data.lastAttempt || 0
3536
const interval = data.syncInterval || DEFAULT_SYNC_INTERVAL
3637
if (data.scheduled) {
3738
await this.ctl.scheduleSync(accountId)
@@ -41,7 +42,7 @@ class AlarmManager {
4142
continue
4243
}
4344
if (data.error && data.errorCount > 1) {
44-
if (Date.now() > this.getBackoffInterval(interval, data.errorCount, lastSync) + lastSync) {
45+
if (Date.now() > this.getBackoffInterval(interval, data.errorCount) + lastAttempt) {
4546
await this.ctl.scheduleSync(accountId)
4647
continue
4748
}
@@ -66,10 +67,9 @@ class AlarmManager {
6667
*
6768
* @param {number} interval - The synchronization interval in minutes.
6869
* @param {number} errorCount - The number of consecutive errors encountered.
69-
* @param {number} lastSync - The timestamp of when the last successful sync happened.
7070
* @returns {number} - The calculated backoff interval in milliseconds.
7171
*/
72-
getBackoffInterval(interval, errorCount, lastSync) {
72+
getBackoffInterval(interval, errorCount) {
7373
return Math.min(MAX_BACKOFF_INTERVAL, interval * 1000 * 60 * Math.pow(2, errorCount))
7474
}
7575
}

src/lib/interfaces/AccountStorage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface IAccountData {
1515
username?: string
1616
password?: string
1717
label?: string
18+
lastSync?: number
19+
lastAttempt?: number
1820
errorCount?: number
1921
clickCountEnabled?: boolean
2022
isTransientError?: boolean|null

src/lib/native/NativeController.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AlarmManager {
4141
const account = await Account.get(accountId)
4242
const data = account.getData()
4343
const lastSync = data.lastSync || 0
44+
const lastAttempt = data.lastAttempt || 0
4445
const interval = data.syncInterval || DEFAULT_SYNC_INTERVAL
4546
if (data.scheduled) {
4647
await this.ctl.scheduleSync(accountId)
@@ -50,7 +51,7 @@ class AlarmManager {
5051
continue
5152
}
5253
if (data.error && data.errorCount > 1) {
53-
if (Date.now() > this.getBackoffInterval(interval, data.errorCount, lastSync) + lastSync) {
54+
if (Date.now() > this.getBackoffInterval(interval, data.errorCount) + lastAttempt) {
5455
await this.ctl.scheduleSync(accountId)
5556
continue
5657
}
@@ -59,7 +60,7 @@ class AlarmManager {
5960
if (
6061
data.syncIntervalEnabled &&
6162
Date.now() >
62-
interval * 1000 * 60 + data.lastSync
63+
interval * 1000 * 60 + lastSync
6364
) {
6465
await this.ctl.scheduleSync(accountId)
6566
}
@@ -75,10 +76,9 @@ class AlarmManager {
7576
*
7677
* @param {number} interval - The synchronization interval in minutes.
7778
* @param {number} errorCount - The number of consecutive errors encountered.
78-
* @param {number} lastSync - The timestamp of when the last successful sync happened.
7979
* @returns {number} - The calculated backoff interval in milliseconds.
8080
*/
81-
getBackoffInterval(interval, errorCount, lastSync) {
81+
getBackoffInterval(interval, errorCount) {
8282
return Math.min(MAX_BACKOFF_INTERVAL, interval * 1000 * 60 * Math.pow(2, errorCount))
8383
}
8484
}

0 commit comments

Comments
 (0)