From d39e13dfe1f0a1a82dbec707d31a41a0f2f3ea52 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Sat, 8 May 2021 22:53:32 +0200 Subject: [PATCH] Add another strategy to logging, which reduces memory leak risk --- package.json | 4 ++-- src/filewriter.ts | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1c33518..4e05ce3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/nodelogging", - "version": "3.0.5", + "version": "3.0.6", "description": "", "main": "out/index.js", "types": "out/index.d.ts", @@ -32,7 +32,7 @@ "typescript": "^4.2.4" }, "dependencies": { - "@hibas123/logging": "^3.0.5", + "@hibas123/logging": "^3.0.6", "@hibas123/utils": "^2.2.18" } } diff --git a/src/filewriter.ts b/src/filewriter.ts index 83ecea5..e24f2f0 100644 --- a/src/filewriter.ts +++ b/src/filewriter.ts @@ -33,6 +33,21 @@ export class LoggingFiles implements Adapter { //TODO: Optimise write path +const Debounce = (callback: () => void, iv = 500) => { + let to: any; + + return { + trigger: () => { + if (!to) { + to = setTimeout(() => { + to = undefined; + callback(); + }, iv); + } + }, + }; +}; + export class Files { private open = 0; @@ -52,9 +67,9 @@ export class Files { private size: number = 0; private stream: fs.WriteStream = undefined; private lock = new Lock(); + private debounce = Debounce(() => this.checkQueue); #initialized = false; - #iv: any; public get initlialized() { return this.#initialized; @@ -68,7 +83,6 @@ export class Files { this.maxFileSize == maxFileSize; await this.initializeFile(); this.#initialized = true; - this.#iv = setInterval(() => this.checkQueue(), 500); lock.release(); this.checkQueue(); } @@ -122,7 +136,6 @@ export class Files { await this.flush(false); this.open--; if (this.open <= 0) { - clearInterval(this.#iv); this.stream.close(); Files.files.delete(this.file); } @@ -164,6 +177,7 @@ export class Files { public write(data: Buffer) { this.queue.push(data); + this.debounce.trigger(); } public dispose() {}