ch.bind.philib.util
Class LeakyBucket

java.lang.Object
  extended by 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
 long canTake()
           
 long canTake(long timeNs)
           
 long getCapacity()
           
 long nextTakeMs()
           
 long nextTakeNs()
           
 long nextTakeNs(long timeNs)
           
 long nextTakeUs()
           
 void sleepUntilAvailable()
           
 void take(long amount)
           
 void take(long amount, long timeNs)
           
static LeakyBucket withTakeIntervalMs(long takeIntervalMs, long capacity)
           
static LeakyBucket withTakeIntervalNs(long takeIntervalNs, long capacity)
           
static LeakyBucket withTakeIntervalUs(long takeIntervalUs, long capacity)
           
static LeakyBucket withTakesPerSecond(double takesPerSecond, long capacity)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 by System.nanoTime() and not System.currentTimeMillis().
Returns:

nextTakeNs

public long nextTakeNs()

nextTakeNs

public long nextTakeNs(long timeNs)
Parameters:
timeNs - a relative timestamp as provided by System.nanoTime() and not System.currentTimeMillis().
Returns:

nextTakeUs

public long nextTakeUs()

nextTakeMs

public long nextTakeMs()

sleepUntilAvailable

public void sleepUntilAvailable()
                         throws InterruptedException
Throws:
InterruptedException


Copyright © 2013. All Rights Reserved.