From 3d9d0a1f6df0aa43ac5a55ed6a62ec38176c8041 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 17 Mar 2013 17:32:46 +0100 Subject: [PATCH] Add time at start of filtering during which no delay will occur. --- .../sonitus/data/filter/RateLimitingFilter.java | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java index 7af827c..7ee4116 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java @@ -42,9 +42,15 @@ public class RateLimitingFilter implements Filter { /** The limiting rate in bytes/second. */ private final int rate; + /** The fast start time. */ + private final long fastStartTime; + /** The source. */ private Source source; + /** The remaining fast start time. */ + private long remainingFastStartTime; + /** * Creates a new rate limiting filter. * @@ -52,7 +58,22 @@ public class RateLimitingFilter implements Filter { * The limiting rate (in bytes/second) */ public RateLimitingFilter(int rate) { + this(rate, 0); + } + + /** + * Creates a new rate limiting filter. + * + * @param rate + * The limiting rate (in bytes/second) + * @param fastStartTime + * The amount of time at the start of the filtering during which no delay + * will occur (in milliseconds) + */ + public RateLimitingFilter(int rate, long fastStartTime) { this.rate = rate; + this.fastStartTime = fastStartTime; + remainingFastStartTime = fastStartTime; } // @@ -70,7 +91,8 @@ public class RateLimitingFilter implements Filter { byte[] buffer = source.get(bufferSize); /* delay. */ long waitTime = 1000 * buffer.length / rate; - while ((System.currentTimeMillis() - now) < waitTime) { + remainingFastStartTime = Math.max(remainingFastStartTime - waitTime, 0); + while ((remainingFastStartTime == 0) && (System.currentTimeMillis() - now) < waitTime) { try { long limitDelay = waitTime - (System.currentTimeMillis() - now); logger.finest(String.format("Waiting %d ms...", limitDelay)); -- 2.7.4