Merge pull request #445 from mempool/simon/disk-cache-save-error
Only allow one disk cache saving simultaneously, and allow for partia…
This commit is contained in:
		
						commit
						f61e3d8cec
					
				| @ -12,6 +12,7 @@ class DiskCache { | |||||||
|   private static FILE_NAME = config.MEMPOOL.CACHE_DIR + '/cache.json'; |   private static FILE_NAME = config.MEMPOOL.CACHE_DIR + '/cache.json'; | ||||||
|   private static FILE_NAMES = config.MEMPOOL.CACHE_DIR + '/cache{number}.json'; |   private static FILE_NAMES = config.MEMPOOL.CACHE_DIR + '/cache{number}.json'; | ||||||
|   private static CHUNK_FILES = 25; |   private static CHUNK_FILES = 25; | ||||||
|  |   private isWritingCache = false; | ||||||
| 
 | 
 | ||||||
|   constructor() { } |   constructor() { } | ||||||
| 
 | 
 | ||||||
| @ -19,8 +20,13 @@ class DiskCache { | |||||||
|     if (!cluster.isMaster) { |     if (!cluster.isMaster) { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|  |     if (this.isWritingCache) { | ||||||
|  |       logger.debug('Saving cache already in progress. Skipping.') | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|     try { |     try { | ||||||
|       logger.debug('Writing mempool and blocks data to disk cache (async)...'); |       logger.debug('Writing mempool and blocks data to disk cache (async)...'); | ||||||
|  |       this.isWritingCache = true; | ||||||
| 
 | 
 | ||||||
|       const mempool = memPool.getMempool(); |       const mempool = memPool.getMempool(); | ||||||
|       const mempoolArray: TransactionExtended[] = []; |       const mempoolArray: TransactionExtended[] = []; | ||||||
| @ -44,8 +50,10 @@ class DiskCache { | |||||||
|         }), {flag: 'w'}); |         }), {flag: 'w'}); | ||||||
|       } |       } | ||||||
|       logger.debug('Mempool and blocks data saved to disk cache'); |       logger.debug('Mempool and blocks data saved to disk cache'); | ||||||
|  |       this.isWritingCache = false; | ||||||
|     } catch (e) { |     } catch (e) { | ||||||
|       logger.warn('Error writing to cache file: ' + e.message || e); |       logger.warn('Error writing to cache file: ' + e.message || e); | ||||||
|  |       this.isWritingCache = false; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -68,6 +76,7 @@ class DiskCache { | |||||||
| 
 | 
 | ||||||
|       for (let i = 1; i < DiskCache.CHUNK_FILES; i++) { |       for (let i = 1; i < DiskCache.CHUNK_FILES; i++) { | ||||||
|         const fileName = DiskCache.FILE_NAMES.replace('{number}', i.toString()); |         const fileName = DiskCache.FILE_NAMES.replace('{number}', i.toString()); | ||||||
|  |         try { | ||||||
|           if (fs.existsSync(fileName)) { |           if (fs.existsSync(fileName)) { | ||||||
|             const cacheData2 = JSON.parse(fs.readFileSync(fileName, 'utf8')); |             const cacheData2 = JSON.parse(fs.readFileSync(fileName, 'utf8')); | ||||||
|             if (cacheData2.mempoolArray) { |             if (cacheData2.mempoolArray) { | ||||||
| @ -78,12 +87,15 @@ class DiskCache { | |||||||
|               Object.assign(data.mempool, cacheData2.mempool); |               Object.assign(data.mempool, cacheData2.mempool); | ||||||
|             } |             } | ||||||
|           } |           } | ||||||
|  |         } catch (e) { | ||||||
|  |           logger.debug('Error parsing ' + fileName + '. Skipping.'); | ||||||
|  |         } | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       memPool.setMempool(data.mempool); |       memPool.setMempool(data.mempool); | ||||||
|       blocks.setBlocks(data.blocks); |       blocks.setBlocks(data.blocks); | ||||||
|     } catch (e) { |     } catch (e) { | ||||||
|       logger.warn('Failed to parse mempoool and blocks cache. Skipping...'); |       logger.warn('Failed to parse mempoool and blocks cache. Skipping.'); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user