Skip to content

Commit

Permalink
Jitter updater poll timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Feb 14, 2024
1 parent 4a41e87 commit fe27910
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions ts/updater/common.ts
Expand Up @@ -25,6 +25,7 @@ import { DialogType } from '../types/Dialogs';
import * as Errors from '../types/errors';
import { isAlpha, isBeta, isStaging } from '../util/version';
import { strictAssert } from '../util/assert';
import { drop } from '../util/drop';

import * as packageJson from '../../package.json';
import {
Expand All @@ -46,7 +47,7 @@ import {
isValidPreparedData as isValidDifferentialData,
} from './differential';

const INTERVAL = 30 * durations.MINUTE;
const POLL_INTERVAL = 30 * durations.MINUTE;

type JSONVendorSchema = {
minOSVersion?: string;
Expand Down Expand Up @@ -148,13 +149,7 @@ export abstract class Updater {
public async start(): Promise<void> {
this.logger.info('updater/start: starting checks...');

setInterval(async () => {
try {
await this.checkForUpdatesMaybeInstall();
} catch (error) {
this.logger.error(`updater/start: ${Errors.toLogFormat(error)}`);
}
}, INTERVAL);
this.schedulePoll();

await this.deletePreviousInstallers();
await this.checkForUpdatesMaybeInstall();
Expand Down Expand Up @@ -216,6 +211,33 @@ export abstract class Updater {
// Private methods
//

private schedulePoll(): void {
const now = Date.now();

const earliestPollTime = now - (now % POLL_INTERVAL) + POLL_INTERVAL;
const selectedPollTime = Math.round(
earliestPollTime + Math.random() * POLL_INTERVAL
);
const timeoutMs = selectedPollTime - now;

this.logger.info(`updater/start: polling in ${timeoutMs}ms`);

setTimeout(() => {
drop(this.safePoll());
}, timeoutMs);
}

private async safePoll(): Promise<void> {
try {
this.logger.info('updater/start: polling now');
await this.checkForUpdatesMaybeInstall();
} catch (error) {
this.logger.error(`updater/start: ${Errors.toLogFormat(error)}`);
} finally {
this.schedulePoll();
}
}

private async downloadAndInstall(
updateInfo: UpdateInformationType,
mode: DownloadMode
Expand Down

0 comments on commit fe27910

Please sign in to comment.