Use s-curve backoff when refreshing github checks

This commit is contained in:
Mattias Granlund 2024-07-13 22:04:36 +01:00
parent f9f79188ed
commit d834f64f34
3 changed files with 32 additions and 16 deletions

View File

@ -0,0 +1,24 @@
import { scurveBackoff } from './scurve';
import { expect, test } from 'vitest';
test('test s-curve backoff 1', () => {
const min = 1000;
const max = 20000;
const initialDelay = scurveBackoff(0, min, max);
expect(initialDelay).toBeGreaterThan(min);
expect(initialDelay).toBeLessThan(1.1 * min);
const finalDelay = scurveBackoff(Number.MAX_SAFE_INTEGER, min, max);
expect(finalDelay).toEqual(max);
});
test('test s-curve backoff 2', () => {
const min = 10000;
const max = 600000;
const initialDelay = scurveBackoff(0, min, max);
expect(initialDelay).toBeGreaterThan(min);
expect(initialDelay).toBeLessThan(1.1 * min);
const finalDelay = scurveBackoff(Number.MAX_SAFE_INTEGER, min, max);
expect(finalDelay).toEqual(max);
});

View File

@ -0,0 +1,6 @@
export function scurveBackoff(ageMs: number, min: number, max: number): number {
// S-curve (sigmoid) with y-axis intercept is at ~10 seconds and max value at 10 minutes.
const delaySeconds =
(max - min) / 1000 / (1 + Math.exp(-(ageMs / 1000 - 2000) / 300)) + min / 1000;
return delaySeconds * 1000;
}

View File

@ -1,3 +1,4 @@
import { scurveBackoff } from '$lib/backoff/scurve';
import { DEFAULT_HEADERS } from '$lib/gitHost/github/headers';
import { parseGitHubCheckSuites } from '$lib/gitHost/github/types';
import { sleep } from '$lib/utils/sleep';
@ -78,9 +79,7 @@ export class GitHubChecksMonitor implements GitHostChecksMonitor {
// to a branch GitHub might not report all the checks that will eventually be
// run as part of the suite.
if (status.completed && ageMs > MIN_COMPLETED_AGE) return;
const backoff = getBackoffByAge(ageMs);
return backoff;
return scurveBackoff(ageMs, 10000, 600000);
}
private async fetchChecksWithRetries(ref: string, retries: number, delayMs: number) {
@ -170,16 +169,3 @@ function parseChecks(
finished
};
}
export function getBackoffByAge(age: number) {
if (age < 60000) {
return 10000;
} else if (age < 600000) {
return 30000;
} else if (age < 1200000) {
return 60000;
} else if (age < 3600000) {
return 120000;
}
return;
}