fix: race condition that results in 2x localReset call the same process

This commit is contained in:
Barış Soner Uşaklı
2024-12-09 18:47:26 -05:00
parent 76caa92c0b
commit c25fd40ae5
2 changed files with 18 additions and 6 deletions

12
src/cache/lru.js vendored
View File

@@ -2,6 +2,8 @@
module.exports = function (opts) {
const { LRUCache } = require('lru-cache');
const os = require('os');
const pubsub = require('../pubsub');
// lru-cache@7 deprecations
@@ -91,7 +93,9 @@ module.exports = function (opts) {
cache.delete = cache.del;
cache.reset = function () {
pubsub.publish(`${cache.name}:lruCache:reset`);
pubsub.publish(`${cache.name}:lruCache:reset`, {
id: `${os.hostname()}:${process.pid}`,
});
localReset();
};
cache.clear = cache.reset;
@@ -102,8 +106,10 @@ module.exports = function (opts) {
cache.misses = 0;
}
pubsub.on(`${cache.name}:lruCache:reset`, () => {
localReset();
pubsub.on(`${cache.name}:lruCache:reset`, ({ id }) => {
if (id !== `${os.hostname()}:${process.pid}`) {
localReset();
}
});
pubsub.on(`${cache.name}:lruCache:del`, (keys) => {

12
src/cache/ttl.js vendored
View File

@@ -2,6 +2,8 @@
module.exports = function (opts) {
const TTLCache = require('@isaacs/ttlcache');
const os = require('os');
const pubsub = require('../pubsub');
const ttlCache = new TTLCache(opts);
@@ -72,7 +74,9 @@ module.exports = function (opts) {
cache.delete = cache.del;
cache.reset = function () {
pubsub.publish(`${cache.name}:ttlCache:reset`);
pubsub.publish(`${cache.name}:ttlCache:reset`, {
id: `${os.hostname()}:${process.pid}`,
});
localReset();
};
cache.clear = cache.reset;
@@ -83,8 +87,10 @@ module.exports = function (opts) {
cache.misses = 0;
}
pubsub.on(`${cache.name}:ttlCache:reset`, () => {
localReset();
pubsub.on(`${cache.name}:ttlCache:reset`, ({ id }) => {
if (id !== `${os.hostname()}:${process.pid}`) {
localReset();
}
});
pubsub.on(`${cache.name}:ttlCache:del`, (keys) => {