From 7fab42baa546a92dbd8d68e9bb2eacf6ae811f11 Mon Sep 17 00:00:00 2001 From: softsimon Date: Mon, 12 Apr 2021 13:21:49 +0400 Subject: [PATCH] Only allow one disk cache saving simultaneously, and allow for partially written cache files. fixes #444 --- backend/src/api/disk-cache.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/backend/src/api/disk-cache.ts b/backend/src/api/disk-cache.ts index 909f12681..ca15bd681 100644 --- a/backend/src/api/disk-cache.ts +++ b/backend/src/api/disk-cache.ts @@ -12,6 +12,7 @@ class DiskCache { private static FILE_NAME = config.MEMPOOL.CACHE_DIR + '/cache.json'; private static FILE_NAMES = config.MEMPOOL.CACHE_DIR + '/cache{number}.json'; private static CHUNK_FILES = 25; + private isWritingCache = false; constructor() { } @@ -19,8 +20,13 @@ class DiskCache { if (!cluster.isMaster) { return; } + if (this.isWritingCache) { + logger.debug('Saving cache already in progress. Skipping.') + return; + } try { logger.debug('Writing mempool and blocks data to disk cache (async)...'); + this.isWritingCache = true; const mempool = memPool.getMempool(); const mempoolArray: TransactionExtended[] = []; @@ -44,8 +50,10 @@ class DiskCache { }), {flag: 'w'}); } logger.debug('Mempool and blocks data saved to disk cache'); + this.isWritingCache = false; } catch (e) { logger.warn('Error writing to cache file: ' + e.message || e); + this.isWritingCache = false; } } @@ -68,22 +76,26 @@ class DiskCache { for (let i = 1; i < DiskCache.CHUNK_FILES; i++) { const fileName = DiskCache.FILE_NAMES.replace('{number}', i.toString()); - if (fs.existsSync(fileName)) { - const cacheData2 = JSON.parse(fs.readFileSync(fileName, 'utf8')); - if (cacheData2.mempoolArray) { - for (const tx of cacheData2.mempoolArray) { - data.mempool[tx.txid] = tx; + try { + if (fs.existsSync(fileName)) { + const cacheData2 = JSON.parse(fs.readFileSync(fileName, 'utf8')); + if (cacheData2.mempoolArray) { + for (const tx of cacheData2.mempoolArray) { + data.mempool[tx.txid] = tx; + } + } else { + Object.assign(data.mempool, cacheData2.mempool); } - } else { - Object.assign(data.mempool, cacheData2.mempool); } + } catch (e) { + logger.debug('Error parsing ' + fileName + '. Skipping.'); } } memPool.setMempool(data.mempool); blocks.setBlocks(data.blocks); } catch (e) { - logger.warn('Failed to parse mempoool and blocks cache. Skipping...'); + logger.warn('Failed to parse mempoool and blocks cache. Skipping.'); } } }