From: David ‘Bombe’ Roden Date: Sun, 17 Mar 2013 16:32:46 +0000 (+0100) Subject: Add time at start of filtering during which no delay will occur. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3d9d0a1f6df0aa43ac5a55ed6a62ec38176c8041;p=sonitus.git Add time at start of filtering during which no delay will occur. --- 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));