From af82be13f9b9a27578c7506c98ef71ccbd84a65c Mon Sep 17 00:00:00 2001 From: Hellyson Rodrigo Parteka Date: Fri, 3 Dec 2021 01:01:35 -0300 Subject: [PATCH] fix(file-cache): add queue to handle eviction of old files This commit also removes the `await` from every stream creation. The eviction will be handled totally assyncronously. The only drawback is the possibility of exceeding the cache limit for a moment, until the next execution of `evictOldest`. This will only be a problem if the cache is set too close to the remaining disk space, which I wouldn't recomend. I also removed the recursion. --- src/services/file-cache.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/services/file-cache.ts b/src/services/file-cache.ts index ddbe190..826c26d 100644 --- a/src/services/file-cache.ts +++ b/src/services/file-cache.ts @@ -5,9 +5,11 @@ import sequelize from 'sequelize'; import {FileCache} from '../models/index.js'; import {TYPES} from '../types.js'; import Config from './config.js'; +import PQueue from 'p-queue'; @injectable() export default class FileCacheProvider { + private static readonly evictionQueue = new PQueue({concurrency: 1}); private readonly config: Config; constructor(@inject(TYPES.Config) config: Config) { @@ -67,7 +69,7 @@ export default class FileCacheProvider { } } - await this.evictOldestIfNecessary(); + this.evictOldestIfNecessary(); }); return stream; @@ -80,10 +82,16 @@ export default class FileCacheProvider { */ async cleanup() { await this.removeOrphans(); - await this.evictOldestIfNecessary(); + this.evictOldestIfNecessary(); } - private async evictOldestIfNecessary() { + private evictOldestIfNecessary() { + if (FileCacheProvider.evictionQueue.size === 0 && FileCacheProvider.evictionQueue.pending === 0) { + void FileCacheProvider.evictionQueue.add(this.evictOldest.bind(this)); + } + } + + private async evictOldest() { const [{dataValues: {totalSizeBytes}}] = await FileCache.findAll({ attributes: [ [sequelize.fn('sum', sequelize.col('bytes')), 'totalSizeBytes'], @@ -103,7 +111,7 @@ export default class FileCacheProvider { } // Continue to evict until we're under the limit - await this.evictOldestIfNecessary(); + void FileCacheProvider.evictionQueue.add(this.evictOldest.bind(this)); } }