Jitter updater poll timeout

This commit is contained in:
Fedor Indutny 2024-02-14 10:27:31 -08:00 committed by GitHub
parent 4a41e87173
commit fe27910221
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -25,6 +25,7 @@ import { DialogType } from '../types/Dialogs';
import * as Errors from '../types/errors'; import * as Errors from '../types/errors';
import { isAlpha, isBeta, isStaging } from '../util/version'; import { isAlpha, isBeta, isStaging } from '../util/version';
import { strictAssert } from '../util/assert'; import { strictAssert } from '../util/assert';
import { drop } from '../util/drop';
import * as packageJson from '../../package.json'; import * as packageJson from '../../package.json';
import { import {
@ -46,7 +47,7 @@ import {
isValidPreparedData as isValidDifferentialData, isValidPreparedData as isValidDifferentialData,
} from './differential'; } from './differential';
const INTERVAL = 30 * durations.MINUTE; const POLL_INTERVAL = 30 * durations.MINUTE;
type JSONVendorSchema = { type JSONVendorSchema = {
minOSVersion?: string; minOSVersion?: string;
@ -148,13 +149,7 @@ export abstract class Updater {
public async start(): Promise<void> { public async start(): Promise<void> {
this.logger.info('updater/start: starting checks...'); this.logger.info('updater/start: starting checks...');
setInterval(async () => { this.schedulePoll();
try {
await this.checkForUpdatesMaybeInstall();
} catch (error) {
this.logger.error(`updater/start: ${Errors.toLogFormat(error)}`);
}
}, INTERVAL);
await this.deletePreviousInstallers(); await this.deletePreviousInstallers();
await this.checkForUpdatesMaybeInstall(); await this.checkForUpdatesMaybeInstall();
@ -216,6 +211,33 @@ export abstract class Updater {
// Private methods // 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( private async downloadAndInstall(
updateInfo: UpdateInformationType, updateInfo: UpdateInformationType,
mode: DownloadMode mode: DownloadMode