From c9d1996e0d0740c0f87a559db85cb8130433c54b Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 1 Mar 2021 18:23:24 +0530 Subject: [PATCH] avoid incrementing index size twice for a single message --- lib/hk-util.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/hk-util.js b/lib/hk-util.js index f141bf35a..b66e7e0b9 100644 --- a/lib/hk-util.js +++ b/lib/hk-util.js @@ -300,6 +300,7 @@ var trimMapByOffset = function (map, offset) { * the fix is to use callbacks and implement queueing for writes * to guarantee that offset computation is always atomic with writes */ +// FIXME 'optionalMessageHash' is always supplied, so we could consider renaming it for clarity const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash, cb) { const id = channel.id; const Log = Env.Log; @@ -338,6 +339,12 @@ const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash, cb) cb(); return void next(); } + + if (optionalMessageHash && typeof(index.offsetByHash[optionalMessageHash]) === 'number') { + cb(); + return void next(); + } + if (typeof (index.line) === "number") { index.line++; } if (isCp) { index.cpIndex = sliceCpIndex(index.cpIndex, index.line || 0); @@ -352,7 +359,7 @@ const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash, cb) can actually cause it to become incorrect, leading to incorrect behaviour when clients connect with a lastKnownHash. We avoid this by only assigning new offsets to the map. */ - if (optionalMessageHash && typeof(index.offsetByHash[optionalMessageHash]) === 'undefined') { + if (optionalMessageHash /* && typeof(index.offsetByHash[optionalMessageHash]) === 'undefined' */) { index.offsetByHash[optionalMessageHash] = index.size; index.offsets++; }