Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cap exponential backoff at 1h #1876

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/lib/browser/BrowserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import * as Sentry from '@sentry/browser'

const INACTIVITY_TIMEOUT = 7 * 1000 // 7 seconds
const MAX_BACKOFF_INTERVAL = 1000 * 60 * 60 // 1 hour
const DEFAULT_SYNC_INTERVAL = 15 // 15 minutes
const STALE_SYNC_TIME = 1000 * 60 * 60 * 24 * 2 // two days
const INTERVENTION_INTERVAL = 1000 * 60 * 60 * 25 * 91 // 91 days
const INTERVENTION_INTERVAL = 1000 * 60 * 60 * 24 * 91 // 91 days

class AlarmManager {
constructor(ctl) {
Expand All @@ -33,8 +34,9 @@
continue
}
if (data.error && data.errorCount > 1) {
if (Date.now() > interval * 2 ** data.errorCount + lastSync) {
if (Date.now() > this.getBackoffInterval(interval, data.errorCount, lastSync) + lastSync) {
promises.push(this.ctl.scheduleSync(accountId))
continue
}
continue
}
Expand All @@ -47,6 +49,27 @@
}
await Promise.all(promises)
}

/**
* Calculates the backoff interval based on the synchronization interval and the error count.
*
* This method determines the delay before retrying a synchronization
* after one or more errors have occurred. It uses an exponential
* backoff algorithm with a cap at the maximum backoff interval.
*
* @param {number} interval - The synchronization interval in minutes.
* @param {number} errorCount - The number of consecutive errors encountered.
* @param {number} lastSync - The timestamp of when the last successful sync happened.
* @returns {number} - The calculated backoff interval in milliseconds.
*/
getBackoffInterval(interval, errorCount, lastSync) {
const maxErrorCount = Math.log2(MAX_BACKOFF_INTERVAL / (interval * 1000 * 60))
if (errorCount < maxErrorCount || lastSync + MAX_BACKOFF_INTERVAL > Date.now()) {
return Math.min(MAX_BACKOFF_INTERVAL, interval * 1000 * 60 * Math.pow(2, errorCount))
} else {
return MAX_BACKOFF_INTERVAL + MAX_BACKOFF_INTERVAL * (errorCount - maxErrorCount)
}
}
}

export default class BrowserController {
Expand Down Expand Up @@ -226,7 +249,7 @@
return Promise.resolve(this.unlocked)
}

async onchange(localId, details) {

Check warning on line 252 in src/lib/browser/BrowserController.js

View workflow job for this annotation

GitHub Actions / js node20.x

'details' is defined but never used

Check warning on line 252 in src/lib/browser/BrowserController.js

View workflow job for this annotation

GitHub Actions / init (20.x, 10.x)

'details' is defined but never used
if (!this.enabled) {
return
}
Expand Down
35 changes: 33 additions & 2 deletions src/lib/native/NativeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Account from '../Account'
import { STATUS_ALLGOOD, STATUS_DISABLED, STATUS_ERROR, STATUS_SYNCING } from '../interfaces/Controller'

const INACTIVITY_TIMEOUT = 1000 * 7
const MAX_BACKOFF_INTERVAL = 1000 * 60 * 60 // 1 hour
const DEFAULT_SYNC_INTERVAL = 15

class AlarmManager {
Expand All @@ -31,18 +32,48 @@ class AlarmManager {
for (let accountId of accounts) {
const account = await Account.get(accountId)
const data = account.getData()
const lastSync = data.lastSync || 0
const interval = data.syncInterval || DEFAULT_SYNC_INTERVAL
if (data.scheduled) {
this.ctl.scheduleSync(accountId)
continue
}
if (data.error && data.errorCount > 1) {
if (Date.now() > this.getBackoffInterval(interval, data.errorCount, lastSync) + lastSync) {
this.ctl.scheduleSync(accountId)
continue
}
continue
}
if (
!data.lastSync ||
Date.now() >
(data.syncInterval || DEFAULT_SYNC_INTERVAL) * 1000 * 60 + data.lastSync
interval * 1000 * 60 + data.lastSync
) {
this.ctl.scheduleSync(accountId)
}
}
}

/**
* Calculates the backoff interval based on the synchronization interval and the error count.
*
* This method determines the delay before retrying a synchronization
* after one or more errors have occurred. It uses an exponential
* backoff algorithm with a cap at the maximum backoff interval.
*
* @param {number} interval - The synchronization interval in minutes.
* @param {number} errorCount - The number of consecutive errors encountered.
* @param {number} lastSync - The timestamp of when the last successful sync happened.
* @returns {number} - The calculated backoff interval in milliseconds.
*/
getBackoffInterval(interval, errorCount, lastSync) {
const maxErrorCount = Math.log2(MAX_BACKOFF_INTERVAL / (interval * 1000 * 60))
if (errorCount < maxErrorCount || lastSync + MAX_BACKOFF_INTERVAL > Date.now()) {
return Math.min(MAX_BACKOFF_INTERVAL, interval * 1000 * 60 * Math.pow(2, errorCount))
} else {
return MAX_BACKOFF_INTERVAL + MAX_BACKOFF_INTERVAL * (errorCount - maxErrorCount)
}
}
}

export default class NativeController {
Expand Down
Loading