From 34aac3958f9baa45b6b2356c4f3316ec19b5b8b0 Mon Sep 17 00:00:00 2001 From: Guifel Date: Tue, 19 Nov 2024 10:37:08 +0100 Subject: [PATCH] Fix race condition https://github.com/JaniAnttonen/winston-loki/issues/117 --- src/batcher.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/batcher.js b/src/batcher.js index af50c40..60b1a7f 100644 --- a/src/batcher.js +++ b/src/batcher.js @@ -253,15 +253,21 @@ class Batcher { } } + const savedBatchStreams = this.batch.streams + + // No need to clear the batch if batching is disabled + logEntry === undefined && this.clearBatch() + // Send the data to Grafana Loki req.post(this.url, this.contentType, this.options.headers, reqBody, this.options.timeout, this.options.httpAgent, this.options.httpsAgent) .then(() => { - // No need to clear the batch if batching is disabled - logEntry === undefined && this.clearBatch() this.batchSent() resolve() }) .catch(err => { + // Revert batch for the logs to be sent on the next iteration + this.batch.streams = savedBatchStreams + // Clear the batch on error if enabled this.options.clearOnError && this.clearBatch()