diff --git a/server/jetstream.go b/server/jetstream.go index 02920e76a4..2e606e6a6f 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 78cd23d6cb..c38d44be49 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)) +}