Skip to content

Commit

Permalink
Cache failed ping requests to avoid hammering any one site too much (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck authored Apr 15, 2021
1 parent 531436a commit 0950b75
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/ping.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
const got = require('got');
const cache = require('./utils/cache');

const ERR_PING_NOT_FOUND = 'ERR_PING_NOT_FOUND';

module.exports = async function (url) {
const cacheKey = `ping_${url}`;

const cacheValue = await cache.get(cacheKey);

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

return undefined;
}

return got
Expand All @@ -17,5 +23,9 @@ module.exports = async function (url) {

return url;
})
.catch(() => undefined);
.catch(async () => {
await cache.set(cacheKey, ERR_PING_NOT_FOUND, 900); // 15 minutes

return undefined;
});
};
8 changes: 4 additions & 4 deletions src/utils/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const redisUrls = {
};

const availableRegions = ['dev1', 'bru1', 'gru1', 'hnd1', 'iad1', 'sfo1'];
const defaultExpireValue = 3600 * 12;

let redis;
const simpleCache = new Map();
Expand Down Expand Up @@ -101,18 +102,17 @@ function auth() {
});
}

async function set(key, value) {
async function set(key, value, expire = defaultExpireValue) {
if (!redis || redis.status !== 'ready') {
log('Cache SET simple-cache', key, value);
simpleCache.set(key, value);
return;
}

try {
log('Cache SET redis-cache', key, value);
const oneHourInSeconds = 3600;
log('Cache SET redis-cache', key, value, expire);
const timingStart = Date.now();
await redis.set(key, value, 'EX', oneHourInSeconds * 12);
await redis.set(key, value, 'EX', expire);
log('Cache SET redis-cache timing', Date.now() - timingStart);
} catch (error) {
log('Cache SET redis-cache error', error);
Expand Down

1 comment on commit 0950b75

@vercel
Copy link

@vercel vercel bot commented on 0950b75 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.