@Test public void checkIntervalTest() throws InterruptedException { time.setCurrentMilliseconds(0); Throttler throttler = new Throttler(100, 10, true, time); throttler.maybeThrottle(150); Assert.assertEquals(0, time.milliseconds()); time.setCurrentMilliseconds(11); throttler.maybeThrottle(0); Assert.assertEquals(1500, time.milliseconds()); time.setCurrentMilliseconds(0); throttler = new Throttler(100, 0, true, time); throttler.maybeThrottle(130); Assert.assertEquals(0, time.milliseconds()); time.setCurrentNanoSeconds(10); throttler.maybeThrottle(20); Assert.assertEquals(1500, time.milliseconds()); time.setCurrentMilliseconds(0); throttler = new Throttler(100, -1, true, time); throttler.maybeThrottle(0); Assert.assertEquals(0, time.milliseconds()); throttler.maybeThrottle(150); Assert.assertEquals(1500, time.milliseconds()); }
public void maybeThrottle(double observed) throws InterruptedException { synchronized (lock) { observedSoFar += observed; long now = time.nanoseconds(); long elapsedNs = now - periodStartNs; if ((checkIntervalMs < 0 || elapsedNs > checkIntervalMs * Time.NsPerMs) && observedSoFar > 0) { double rateInSecs = elapsedNs > 0 ? (observedSoFar * Time.NsPerSec) / elapsedNs : Double.MAX_VALUE; if (throttleDown == rateInSecs > desiredRatePerSec) { double desiredRateMs = desiredRatePerSec / Time.MsPerSec; double elapsedMs = elapsedNs / Time.NsPerMs; long sleepTime = Math.round(observedSoFar / desiredRateMs - elapsedMs); if (sleepTime > 0) { logger.trace("Natural rate is {} per second but desired rate is {}, sleeping for {} ms to compensate.", rateInSecs, desiredRatePerSec, sleepTime); synchronized (waitGuard) { if (enabled) { time.wait(waitGuard, sleepTime); } } } } periodStartNs = now; observedSoFar = 0; } } }
Throttler { public void maybeThrottle(double observed) throws InterruptedException { synchronized (lock) { observedSoFar += observed; long now = time.nanoseconds(); long elapsedNs = now - periodStartNs; if ((checkIntervalMs < 0 || elapsedNs > checkIntervalMs * Time.NsPerMs) && observedSoFar > 0) { double rateInSecs = elapsedNs > 0 ? (observedSoFar * Time.NsPerSec) / elapsedNs : Double.MAX_VALUE; if (throttleDown == rateInSecs > desiredRatePerSec) { double desiredRateMs = desiredRatePerSec / Time.MsPerSec; double elapsedMs = elapsedNs / Time.NsPerMs; long sleepTime = Math.round(observedSoFar / desiredRateMs - elapsedMs); if (sleepTime > 0) { logger.trace("Natural rate is {} per second but desired rate is {}, sleeping for {} ms to compensate.", rateInSecs, desiredRatePerSec, sleepTime); synchronized (waitGuard) { if (enabled) { time.wait(waitGuard, sleepTime); } } } } periodStartNs = now; observedSoFar = 0; } } } }
Throttler { public void maybeThrottle(double observed) throws InterruptedException { synchronized (lock) { observedSoFar += observed; long now = time.nanoseconds(); long elapsedNs = now - periodStartNs; if ((checkIntervalMs < 0 || elapsedNs > checkIntervalMs * Time.NsPerMs) && observedSoFar > 0) { double rateInSecs = elapsedNs > 0 ? (observedSoFar * Time.NsPerSec) / elapsedNs : Double.MAX_VALUE; if (throttleDown == rateInSecs > desiredRatePerSec) { double desiredRateMs = desiredRatePerSec / Time.MsPerSec; double elapsedMs = elapsedNs / Time.NsPerMs; long sleepTime = Math.round(observedSoFar / desiredRateMs - elapsedMs); if (sleepTime > 0) { logger.trace("Natural rate is {} per second but desired rate is {}, sleeping for {} ms to compensate.", rateInSecs, desiredRatePerSec, sleepTime); synchronized (waitGuard) { if (enabled) { time.wait(waitGuard, sleepTime); } } } } periodStartNs = now; observedSoFar = 0; } } } Throttler(double desiredRatePerSec, long checkIntervalMs, boolean throttleDown, Time time); }
Throttler { public void maybeThrottle(double observed) throws InterruptedException { synchronized (lock) { observedSoFar += observed; long now = time.nanoseconds(); long elapsedNs = now - periodStartNs; if ((checkIntervalMs < 0 || elapsedNs > checkIntervalMs * Time.NsPerMs) && observedSoFar > 0) { double rateInSecs = elapsedNs > 0 ? (observedSoFar * Time.NsPerSec) / elapsedNs : Double.MAX_VALUE; if (throttleDown == rateInSecs > desiredRatePerSec) { double desiredRateMs = desiredRatePerSec / Time.MsPerSec; double elapsedMs = elapsedNs / Time.NsPerMs; long sleepTime = Math.round(observedSoFar / desiredRateMs - elapsedMs); if (sleepTime > 0) { logger.trace("Natural rate is {} per second but desired rate is {}, sleeping for {} ms to compensate.", rateInSecs, desiredRatePerSec, sleepTime); synchronized (waitGuard) { if (enabled) { time.wait(waitGuard, sleepTime); } } } } periodStartNs = now; observedSoFar = 0; } } } Throttler(double desiredRatePerSec, long checkIntervalMs, boolean throttleDown, Time time); void maybeThrottle(double observed); void disable(); }
Throttler { public void maybeThrottle(double observed) throws InterruptedException { synchronized (lock) { observedSoFar += observed; long now = time.nanoseconds(); long elapsedNs = now - periodStartNs; if ((checkIntervalMs < 0 || elapsedNs > checkIntervalMs * Time.NsPerMs) && observedSoFar > 0) { double rateInSecs = elapsedNs > 0 ? (observedSoFar * Time.NsPerSec) / elapsedNs : Double.MAX_VALUE; if (throttleDown == rateInSecs > desiredRatePerSec) { double desiredRateMs = desiredRatePerSec / Time.MsPerSec; double elapsedMs = elapsedNs / Time.NsPerMs; long sleepTime = Math.round(observedSoFar / desiredRateMs - elapsedMs); if (sleepTime > 0) { logger.trace("Natural rate is {} per second but desired rate is {}, sleeping for {} ms to compensate.", rateInSecs, desiredRatePerSec, sleepTime); synchronized (waitGuard) { if (enabled) { time.wait(waitGuard, sleepTime); } } } } periodStartNs = now; observedSoFar = 0; } } } Throttler(double desiredRatePerSec, long checkIntervalMs, boolean throttleDown, Time time); void maybeThrottle(double observed); void disable();  }