Skip to content

Commit

Permalink
Cache failed registry lookups to avoid hammering APIs (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck authored Apr 15, 2021
1 parent 0950b75 commit 4b8d765
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/registries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ const cache = require('../utils/cache');
const log = require('../utils/log');
const { prioritiesHost } = require('../utils/url');

const ERR_PACKAGE_NOT_FOUND = 'ERR_PACKAGE_NOT_FOUND';

async function resolve(type, packageName) {
const cacheKey = `${type}_${packageName}`;

const cacheValue = await cache.get(cacheKey);

if (cacheValue) {
return cacheValue;
if (cacheValue !== ERR_PACKAGE_NOT_FOUND) {
return cacheValue;
}

return undefined;
}

const config = registryConfig[type];
Expand All @@ -34,7 +40,9 @@ async function resolve(type, packageName) {
response = await got.get(requestUrl);
} catch (err) {
if (err.statusCode === 404) {
return log('Package not found', packageName, type);
log('Package not found', packageName, type);
await cache.set(cacheKey, ERR_PACKAGE_NOT_FOUND, 900); // 15 minutes
return;
}

return log(err);
Expand All @@ -45,6 +53,7 @@ async function resolve(type, packageName) {
json = JSON.parse(response.body);
} catch (err) {
log('Parsing response failed');
await cache.set(cacheKey, ERR_PACKAGE_NOT_FOUND, 900); // 15 minutes
return;
}

Expand Down Expand Up @@ -94,6 +103,7 @@ async function resolve(type, packageName) {

if (!reachableUrl) {
log('No URL for package found');
await cache.set(cacheKey, ERR_PACKAGE_NOT_FOUND, 900); // 15 minutes
return;
}

Expand Down

1 comment on commit 4b8d765

@vercel
Copy link

@vercel vercel bot commented on 4b8d765 Apr 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.