Skip to content

Commit 5bc3593

Browse files
authored
Use retries for bestVersion (#338)
The HttpClient only retries _responses_. If there are connection disconnects (e.g. google-github-actions/setup-gcloud#714), retries don't happen.
1 parent 94073ab commit 5bc3593

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/index.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { HttpClient } from '@actions/http-client';
2323
import * as core from '@actions/core';
2424
import * as toolCache from '@actions/tool-cache';
2525
import * as semver from 'semver';
26-
import { errorMessage } from '@google-github-actions/actions-utils';
26+
import { errorMessage, withRetries } from '@google-github-actions/actions-utils';
2727

2828
import { buildReleaseURL } from './format-url';
2929
import { downloadAndExtractTool } from './download-util';
@@ -269,23 +269,32 @@ export async function getLatestGcloudSDKVersion(): Promise<string> {
269269
*/
270270
export async function bestVersion(spec: string): Promise<string> {
271271
let versions: string[];
272-
try {
273-
const http = new HttpClient(userAgentString, undefined, { allowRetries: true, maxRetries: 3 });
274-
const res = await http.get(versionsURL);
275-
276-
const body = await res.readBody();
277-
const statusCode = res.message.statusCode || 500;
278-
if (statusCode >= 400) {
279-
throw new Error(`(${statusCode}) ${body}`);
280-
}
281272

282-
versions = JSON.parse(body) as string[];
273+
try {
274+
return await withRetries(
275+
async (): Promise<string> => {
276+
const http = new HttpClient(userAgentString);
277+
const res = await http.get(versionsURL);
278+
279+
const body = await res.readBody();
280+
const statusCode = res.message.statusCode || 500;
281+
if (statusCode >= 400) {
282+
throw new Error(`(${statusCode}) ${body}`);
283+
}
284+
285+
versions = JSON.parse(body) as string[];
286+
return computeBestVersion(spec, versions);
287+
},
288+
{
289+
retries: 3,
290+
backoff: 100, // 100 milliseconds
291+
backoffLimit: 1_000, // 1 second
292+
},
293+
)();
283294
} catch (err) {
284295
const msg = errorMessage(err);
285296
throw new Error(`failed to retrieve versions from ${versionsURL}: ${msg}`);
286297
}
287-
288-
return computeBestVersion(spec, versions);
289298
}
290299

291300
/**

0 commit comments

Comments
 (0)