Skip to content

Commit 0c0448c

Browse files
committed
feat: Instead of sleeping a fixed 5 seconds between requests, wait with exponential backoff and retries
1 parent 2bcbbdc commit 0c0448c

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

.github/actions/fix/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Octokit } from "@octokit/core";
55
import { throttling } from "@octokit/plugin-throttling";
66
import { assignIssue } from "./assignIssue.js";
77
import { getLinkedPR } from "./getLinkedPR.js";
8-
import { sleep } from "./sleep.js";
8+
import { retry } from "./retry.js";
99
import { Issue } from "./Issue.js";
1010
const OctokitWithThrottling = Octokit.plugin(throttling);
1111

@@ -51,8 +51,7 @@ export default async function () {
5151
core.info(
5252
`Assigned ${issue.owner}/${issue.repository}#${issue.issueNumber} to Copilot!`
5353
);
54-
await sleep(5000); // Wait for Copilot to open a PR
55-
const pullRequest = await getLinkedPR(octokit, issue);
54+
const pullRequest = await retry(() => getLinkedPR(octokit, issue));
5655
if (pullRequest) {
5756
fixing.pullRequest = pullRequest;
5857
core.info(

.github/actions/fix/src/retry.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Sleep for a given number of milliseconds.
3+
* @param ms Time to sleep, in milliseconds.
4+
*/
5+
function sleep(ms: number): Promise<void> {
6+
return new Promise((resolve) => setTimeout(() => resolve(), ms));
7+
}
8+
9+
/**
10+
* Retry a function with exponential backoff.
11+
* @param fn The function to retry.
12+
* @param maxAttempts The maximum number of retry attempts.
13+
* @param baseDelay The base delay between attempts.
14+
* @param attempt The current attempt number.
15+
* @returns The result of the function or undefined if all attempts fail.
16+
*/
17+
export async function retry<T>(
18+
fn: () => Promise<T | null | undefined> | T | null | undefined,
19+
maxAttempts = 6,
20+
baseDelay = 2000,
21+
attempt = 1
22+
): Promise<T | undefined> {
23+
const value = await fn();
24+
if (value != null) return value;
25+
if (attempt >= maxAttempts) return undefined;
26+
/** Exponential backoff, capped at 30s */
27+
const delay = Math.min(30000, baseDelay * 2 ** (attempt - 1));
28+
/** ±10% jitter */
29+
const jitter = 1 + (Math.random() - 0.5) * 0.2;
30+
await sleep(Math.round(delay * jitter));
31+
return retry(fn, maxAttempts, baseDelay, attempt + 1);
32+
}

.github/actions/fix/src/sleep.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)