Skip to content

Commit

Permalink
feat(gomod): Add GOPROXY caching (#12232)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Oct 21, 2021
1 parent b227b94 commit 2c78d9a
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/datasource/go/goproxy.ts
Expand Up @@ -2,6 +2,7 @@ import is from '@sindresorhus/is';
import moo from 'moo';
import pAll from 'p-all';
import { logger } from '../../logger';
import * as packageCache from '../../util/cache/package';
import { regEx } from '../../util/regex';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import { GoproxyFallback, http } from './common';
Expand Down Expand Up @@ -166,7 +167,22 @@ export async function getReleases(
return null;
}

const proxyList = parseGoproxy();
const goproxy = process.env.GOPROXY;
const proxyList = parseGoproxy(goproxy);

const cacheNamespaces = 'datasource-go-proxy';
const cacheKey = `${lookupName}@@${goproxy}`;
const cacheMinutes = 60;
const cachedResult = await packageCache.get<ReleaseResult | null>(
cacheNamespaces,
cacheKey
);
// istanbul ignore if
if (cachedResult || cachedResult === null) {
return cachedResult;
}

let result: ReleaseResult | null = null;

for (const { url, fallback } of proxyList) {
try {
Expand All @@ -181,7 +197,8 @@ export async function getReleases(
});
const releases = await pAll(queue, { concurrency: 5 });
if (releases.length) {
return { releases };
result = { releases };
break;
}
} catch (err) {
const statusCode = err?.response?.statusCode;
Expand All @@ -199,5 +216,6 @@ export async function getReleases(
}
}

return null;
await packageCache.set(cacheNamespaces, cacheKey, result, cacheMinutes);
return result;
}

0 comments on commit 2c78d9a

Please sign in to comment.