Merge pull request #1330 from balena-io/avoid-instant-updates-query

Avoid querying `instantUpdates` on every state poll
This commit is contained in:
Page- 2020-05-18 14:11:17 +01:00 committed by GitHub
commit bb1e0cdfcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -354,7 +354,16 @@ export class APIBinder {
'Trying to start poll without initializing API client',
);
}
this.pollTargetState(true);
this.config
.get('instantUpdates')
.catch(() => {
// Default to skipping the initial update if we couldn't fetch the setting
// which should be the exceptional case
return false;
})
.then((instantUpdates) => {
this.pollTargetState(!instantUpdates);
});
return null;
});
}
@ -602,18 +611,17 @@ export class APIBinder {
});
}
private async pollTargetState(isInitialCall: boolean = false): Promise<void> {
const {
appUpdatePollInterval,
instantUpdates,
} = await this.config.getMany(['appUpdatePollInterval', 'instantUpdates']);
private async pollTargetState(skipFirstGet: boolean = false): Promise<void> {
const appUpdatePollInterval = await this.config.get(
'appUpdatePollInterval',
);
// We add a random jitter up to `maxApiJitterDelay` to
// space out poll requests
let pollInterval =
Math.random() * constants.maxApiJitterDelay + appUpdatePollInterval;
if (instantUpdates || !isInitialCall) {
if (!skipFirstGet) {
try {
await this.getAndSetTargetState(false);
this.targetStateFetchErrors = 0;