mirror of
				https://git.hibas.dev/OpenServer/NodeLogging.git
				synced 2025-11-03 22:40:45 +00:00 
			
		
		
		
	Add another strategy to logging, which reduces memory leak risk
This commit is contained in:
		@ -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() {}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user