diff options
| author | Hellyson Rodrigo Parteka <[email protected]> | 2021-12-03 01:01:35 -0300 |
|---|---|---|
| committer | Hellyson Rodrigo Parteka <[email protected]> | 2021-12-03 01:01:35 -0300 |
| commit | af82be13f9b9a27578c7506c98ef71ccbd84a65c (patch) | |
| tree | 1f9cfc8879f0ffa39e84f0d65c3e490f1ac2fedd /src/services/file-cache.ts | |
| parent | 3b2aa47e95e277e94a053636f693801c8dc96cf4 (diff) | |
| download | muse-af82be13f9b9a27578c7506c98ef71ccbd84a65c.tar.xz muse-af82be13f9b9a27578c7506c98ef71ccbd84a65c.zip | |
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.
Diffstat (limited to 'src/services/file-cache.ts')
| -rw-r--r-- | src/services/file-cache.ts | 16 |
1 files 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)); } } |
