From 300cf79c95be43d79633ddcf9f28314c7a05683b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 4 Jun 2024 12:31:13 -0400 Subject: [PATCH] add max to caches to limit memory usage --- src/activitypub/actors.js | 5 ++++- src/activitypub/helpers.js | 10 +++++++--- src/activitypub/index.js | 10 +++++++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/activitypub/actors.js b/src/activitypub/actors.js index 628246464e..6f25d67ae0 100644 --- a/src/activitypub/actors.js +++ b/src/activitypub/actors.js @@ -8,7 +8,10 @@ const user = require('../user'); const utils = require('../utils'); const TTLCache = require('../cache/ttl'); -const failedWebfingerCache = TTLCache({ ttl: 1000 * 60 * 10 }); // 10 minutes +const failedWebfingerCache = TTLCache({ + max: 5000, + ttl: 1000 * 60 * 10, // 10 minutes +}); const activitypub = module.parent.exports; diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.js index 967520ca3f..37a5fdbcc5 100644 --- a/src/activitypub/helpers.js +++ b/src/activitypub/helpers.js @@ -16,7 +16,10 @@ const user = require('../user'); const activitypub = require('.'); const webfingerRegex = /^(@|acct:)?[\w\-]+@.+$/; -const webfingerCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours +const webfingerCache = ttl({ + max: 5000, + ttl: 1000 * 60 * 60 * 24, // 24 hours +}); const Helpers = module.exports; @@ -59,8 +62,9 @@ Helpers.query = async (id) => { return false; } - if (webfingerCache.has(id)) { - return webfingerCache.get(id); + const cached = webfingerCache.get(id); + if (cached !== undefined) { + return cached; } const query = new URLSearchParams({ resource: uri }); diff --git a/src/activitypub/index.js b/src/activitypub/index.js index 0e46941bb7..25ddcbe3ef 100644 --- a/src/activitypub/index.js +++ b/src/activitypub/index.js @@ -15,7 +15,10 @@ const batch = require('../batch'); const pubsub = require('../pubsub'); const analytics = require('../analytics'); -const requestCache = ttl({ ttl: 1000 * 60 * 5 }); // 5 minutes +const requestCache = ttl({ + max: 5000, + ttl: 1000 * 60 * 5, // 5 minutes +}); const ActivityPub = module.exports; ActivityPub._constants = Object.freeze({ @@ -223,8 +226,9 @@ ActivityPub.verify = async (req) => { ActivityPub.get = async (type, id, uri) => { const cacheKey = [id, uri].join(';'); - if (requestCache.has(cacheKey)) { - return requestCache.get(cacheKey); + const cached = requestCache.get(cacheKey); + if (cached !== undefined) { + return cached; } const keyData = await ActivityPub.getPrivateKey(type, id);