Skip to content

Commit dfc1411

Browse files
Change waitForProcessing to use exponential backoff
1 parent 0e150e4 commit dfc1411

2 files changed

Lines changed: 40 additions & 32 deletions

File tree

lib/entry-points.js

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/upload-lib.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -829,8 +829,10 @@ function dumpSarifFile(
829829
fs.writeFileSync(outputFile, sarifPayload);
830830
}
831831

832-
const STATUS_CHECK_FREQUENCY_MILLISECONDS = 5 * 1000;
833-
const STATUS_CHECK_TIMEOUT_MILLISECONDS = 2 * 60 * 1000;
832+
// Should lead to status checks after 5s, 15s, 35s, 75s, and 155s.
833+
const STATUS_CHECK_INITIAL_BACKOFF_MILLISECONDS = 5 * 1000;
834+
const STATUS_CHECK_BACKOFF_MULTIPLIER = 2;
835+
const STATUS_CHECK_MAX_TRIES = 5;
834836

835837
type ProcessingStatus = "pending" | "complete" | "failed";
836838

@@ -854,20 +856,15 @@ export async function waitForProcessing(
854856
try {
855857
const client = api.getApiClient();
856858

857-
const statusCheckingStarted = Date.now();
858-
while (true) {
859-
if (
860-
Date.now() >
861-
statusCheckingStarted + STATUS_CHECK_TIMEOUT_MILLISECONDS
862-
) {
863-
// If the analysis hasn't finished processing in the allotted time, we continue anyway rather than failing.
864-
// It's possible the analysis will eventually finish processing, but it's not worth spending more
865-
// Actions time waiting.
866-
logger.warning(
867-
"Timed out waiting for analysis to finish processing. Continuing.",
868-
);
869-
break;
870-
}
859+
// Do an initial wait because processing will always take a minimum of 2-3 seconds
860+
let statusCheckBackoff = STATUS_CHECK_INITIAL_BACKOFF_MILLISECONDS;
861+
await util.delay(statusCheckBackoff, { allowProcessExit: false });
862+
863+
for (
864+
let statusCheckCount = 1;
865+
statusCheckCount <= STATUS_CHECK_MAX_TRIES;
866+
statusCheckCount++
867+
) {
871868
let response: OctokitResponse<any> | undefined = undefined;
872869
try {
873870
response = await client.request(
@@ -912,9 +909,18 @@ export async function waitForProcessing(
912909
util.assertNever(status);
913910
}
914911

915-
await util.delay(STATUS_CHECK_FREQUENCY_MILLISECONDS, {
916-
allowProcessExit: false,
917-
});
912+
if (statusCheckCount === STATUS_CHECK_MAX_TRIES) {
913+
// If the analysis hasn't finished processing in the allotted time, we continue anyway rather than failing.
914+
// It's possible the analysis will eventually finish processing, but it's not worth spending more
915+
// Actions time waiting.
916+
logger.warning(
917+
"Timed out waiting for analysis to finish processing. Continuing.",
918+
);
919+
break;
920+
} else {
921+
statusCheckBackoff *= STATUS_CHECK_BACKOFF_MULTIPLIER;
922+
await util.delay(statusCheckBackoff, { allowProcessExit: false });
923+
}
918924
}
919925
} finally {
920926
logger.endGroup();

0 commit comments

Comments
 (0)