From cc4e175011647143d3771318c8ab690892360e60 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 a43cc67eb80..a0b31cd6387 100644 --- a/server/jetstream.go +++ b/server/jetstream.go @@ -2163,7 +2163,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 66b7566fb05..d5d20ae89cd 100644 --- a/server/jetstream_test.go +++ b/server/jetstream_test.go @@ -24721,3 +24721,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)) +}