diff --git a/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/window/ImmutablePercentileSampleWindow.java b/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/window/ImmutablePercentileSampleWindow.java index e5044a6a..967a24b5 100644 --- a/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/window/ImmutablePercentileSampleWindow.java +++ b/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/window/ImmutablePercentileSampleWindow.java @@ -36,7 +36,7 @@ class ImmutablePercentileSampleWindow implements SampleWindow { this.percentile = percentile; } - ImmutablePercentileSampleWindow( + private ImmutablePercentileSampleWindow( long minRtt, int maxInFlight, boolean didDrop, @@ -54,6 +54,9 @@ class ImmutablePercentileSampleWindow implements SampleWindow { @Override public ImmutablePercentileSampleWindow addSample(long rtt, int inflight, boolean didDrop) { + if (sampleCount >= observedRtts.length()) { + return this; + } observedRtts.set(sampleCount, rtt); return new ImmutablePercentileSampleWindow( Math.min(minRtt, rtt), @@ -72,6 +75,9 @@ public long getCandidateRttNanos() { @Override public long getTrackedRttNanos() { + if (sampleCount == 0) { + return 0; + } long[] copyOfObservedRtts = new long[sampleCount]; for (int i = 0; i < sampleCount; i++) { copyOfObservedRtts[i] = observedRtts.get(i); diff --git a/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/window/PercentileSampleWindowFactory.java b/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/window/PercentileSampleWindowFactory.java index 2bca2a4d..79ceda23 100644 --- a/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/window/PercentileSampleWindowFactory.java +++ b/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/window/PercentileSampleWindowFactory.java @@ -33,6 +33,6 @@ public static PercentileSampleWindowFactory of(double percentile, int windowSize @Override public ImmutablePercentileSampleWindow newInstance() { - return new ImmutablePercentileSampleWindow(percentile, (int) (windowSize * 1.2)); + return new ImmutablePercentileSampleWindow(percentile, windowSize); } }