- java.lang.Object
-
- ch.bind.philib.util.LeakyBucket
-
public final class LeakyBucket extends Object
An implementation of the leaky bucket pattern for throughput control.- Author:
- Philipp Meinen
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description longcanTake()longcanTake(long timeNs)longgetCapacity()longnextTakeMs()longnextTakeNs()longnextTakeNs(long timeNs)longnextTakeUs()voidsleepUntilAvailable()voidtake(long amount)voidtake(long amount, long timeNs)static LeakyBucketwithTakeIntervalMs(long takeIntervalMs, long capacity)static LeakyBucketwithTakeIntervalNs(long takeIntervalNs, long capacity)static LeakyBucketwithTakeIntervalUs(long takeIntervalUs, long capacity)static LeakyBucketwithTakesPerSecond(double takesPerSecond, long capacity)
-
-
-
Method Detail
-
withTakesPerSecond
public static LeakyBucket withTakesPerSecond(double takesPerSecond, long capacity)
-
withTakeIntervalMs
public static LeakyBucket withTakeIntervalMs(long takeIntervalMs, long capacity)
-
withTakeIntervalUs
public static LeakyBucket withTakeIntervalUs(long takeIntervalUs, long capacity)
-
withTakeIntervalNs
public static LeakyBucket withTakeIntervalNs(long takeIntervalNs, long capacity)
-
getCapacity
public long getCapacity()
-
take
public void take(long amount)
-
take
public void take(long amount, long timeNs)
-
canTake
public long canTake()
-
canTake
public long canTake(long timeNs)
- Parameters:
timeNs- a relative timestamp as provided bySystem.nanoTime()and notSystem.currentTimeMillis().- Returns:
- the number of items that can be taken immediately
-
nextTakeNs
public long nextTakeNs()
-
nextTakeNs
public long nextTakeNs(long timeNs)
- Parameters:
timeNs- a relative timestamp as provided bySystem.nanoTime()and notSystem.currentTimeMillis().- Returns:
- the number of nanoseconds until an item can be taken
-
nextTakeUs
public long nextTakeUs()
-
nextTakeMs
public long nextTakeMs()
-
sleepUntilAvailable
public void sleepUntilAvailable() throws InterruptedException- Throws:
InterruptedException
-
-