From ec1a7b4710a090d128f5980dad8d4b7704b1d625 Mon Sep 17 00:00:00 2001 From: Maurice van Veen Date: Mon, 16 Dec 2024 14:45:19 +0100 Subject: [PATCH] [FIXED] JetStream would exceed limits calculation Signed-off-by: Maurice van Veen --- server/jetstream.go | 2 +- server/jetstream_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/server/jetstream.go b/server/jetstream.go index 02920e76a4b..2e606e6a6fa 100644 --- a/server/jetstream.go +++ b/server/jetstream.go @@ -2107,7 +2107,7 @@ func (js *jetStream) wouldExceedLimits(storeType StorageType, sz int) bool { } else { total, max = &js.storeUsed, js.config.MaxStore } - return atomic.LoadInt64(total) > (max + int64(sz)) + return (atomic.LoadInt64(total) + int64(sz)) > max } func (js *jetStream) limitsExceeded(storeType StorageType) bool { diff --git a/server/jetstream_test.go b/server/jetstream_test.go index 78cd23d6cbb..c38d44be492 100644 --- a/server/jetstream_test.go +++ b/server/jetstream_test.go @@ -22981,3 +22981,19 @@ func TestJetStreamMemoryPurgeClearsSubjectsState(t *testing.T) { require_NoError(t, err) require_Len(t, len(si.State.Subjects), 0) } + +func TestJetStreamWouldExceedLimits(t *testing.T) { + s := RunBasicJetStreamServer(t) + defer s.Shutdown() + + js := s.getJetStream() + require_NotNil(t, js) + + // Storing exactly up to the limit should work. + require_False(t, js.wouldExceedLimits(MemoryStorage, int(js.config.MaxMemory))) + require_False(t, js.wouldExceedLimits(FileStorage, int(js.config.MaxStore))) + + // Storing one more than the max should exceed limits. + require_True(t, js.wouldExceedLimits(MemoryStorage, int(js.config.MaxMemory)+1)) + require_True(t, js.wouldExceedLimits(FileStorage, int(js.config.MaxStore)+1)) +}