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

Implement exponential backoff for polling; improve logging #5163

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ import {AdminSession} from '@shopify/cli-kit/node/session'
import {renderFatalError} from '@shopify/cli-kit/node/ui'
import {AbortError} from '@shopify/cli-kit/node/error'

const POLLING_INTERVAL = 3000
const BASE_POLLING_INTERVAL = 3000
// Maximum backoff of 60 seconds
const MAX_POLLING_INTERVAL = 60000
// Add up to 30% random jitter
const JITTER_FACTOR = 0.3
class PollingError extends Error {}

function calculateBackoffTime(attemptNumber: number): number {
// Calculate exponential backoff: BASE * 2^attempt
const exponentialDelay = BASE_POLLING_INTERVAL * 2 ** attemptNumber
// Cap it at the maximum interval
const cappedDelay = Math.min(exponentialDelay, MAX_POLLING_INTERVAL)
// Add random jitter
const jitter = cappedDelay * JITTER_FACTOR * Math.random()
return cappedDelay + jitter
}

export interface PollingOptions {
noDelete: boolean
}
Expand All @@ -32,17 +46,21 @@ export function pollThemeEditorChanges(
const poll = async () => {
// Asynchronously wait for the polling interval, similar to a setInterval
// but ensure the polling work is done before starting the next interval.
await new Promise((resolve) => setTimeout(resolve, POLLING_INTERVAL))
const backoffTime = calculateBackoffTime(failedPollingAttempts)
await new Promise((resolve) => setTimeout(resolve, backoffTime))

outputDebug('Fetching remote checksums')
// eslint-disable-next-line require-atomic-updates
latestChecksums = await pollRemoteJsonChanges(targetTheme, session, latestChecksums, localFileSystem, options)
.then((checksums) => {
outputDebug('Succesfully fetched checksums')
failedPollingAttempts = 0
lastError = ''

return checksums
})
.catch((error: Error) => {
outputDebug(`Error fetching checksums: ${error}`)
failedPollingAttempts++

if (error.message !== lastError) {
Expand Down
Loading