net.kano.joscar.ratelim
Class RateClassMonitor

java.lang.Object
  extended byRateClassMonitor

public class RateClassMonitor
extends java.lang.Object

Monitors rate information for a single rate class.


Method Summary
 int getErrorMargin()
          Returns this monitor's error margin.
 long getLastRateAvg()
          Returns what the rate average was when the last command in the associated rate class was sent.
 int getLocalErrorMargin()
          Returns this monitor's locally set error margin.
 int getMaxCmdCount()
          Returns the maximum number of commands in this rate class that could ever be sent at the same time without being rate-limited.
 long getOptimalWaitTime()
          Returns how long one "should" wait before sending a command in this monitor's associated rate class (to avoid being rate limited).
 int getPossibleCmdCount()
          Returns the number of commands in this rate class that could be sent immediately, without being rate limited.
 long getPotentialAvg()
          Returns what the rate average would be if a command were sent at the current time.
 long getPotentialAvg(long time)
          Returns what the rate average would be if a command were sent at the given time.
 RateClassInfo getRateInfo()
          Returns the rate information associated with this monitor's associated rate class.
 long getTimeUntil(long minAvg)
          Returns how long one must wait before sending a command in this monitor's associated rate class to keep the current average above the given average.
 boolean isLimited()
          Returns whether this rate monitor's associated rate class is currently rate-limited.
 void setErrorMargin(int errorMargin)
          Sets this monitor's error margin.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getRateInfo

public final RateClassInfo getRateInfo()
Returns the rate information associated with this monitor's associated rate class.

Returns:
this monitor's associated rate class's rate information

getErrorMargin

public final int getErrorMargin()
Returns this monitor's error margin. If this monitor's error margin is set to -1, the error margin of this monitor's parent RateMonitor will be returned.

Returns:
this monitor's error margin

getLocalErrorMargin

public final int getLocalErrorMargin()
Returns this monitor's locally set error margin. This value defaults to -1, which indicates that the error margin should be "inherited" from this rate class monitor's parent RateMonitor.

Returns:
this monitor's locally set error margin, or -1 if this monitor's error margin is currently inherited from its parent rate monitor

setErrorMargin

public final void setErrorMargin(int errorMargin)
Sets this monitor's error margin. Note that if the given margin is -1 this monitor's error margin will be "inherited" from this monitor's parent RateMonitor.

Parameters:
errorMargin - an error margin value

isLimited

public final boolean isLimited()
Returns whether this rate monitor's associated rate class is currently rate-limited.

Returns:
whether this rate monitor's associated rate class is currently rate-limited

getLastRateAvg

public final long getLastRateAvg()
Returns what the rate average was when the last command in the associated rate class was sent.

Returns:
the rate average at the time of the last command send

getPotentialAvg

public final long getPotentialAvg()
Returns what the rate average would be if a command were sent at the current time.

Returns:
the potential rate average

getPotentialAvg

public final long getPotentialAvg(long time)
Returns what the rate average would be if a command were sent at the given time.

Parameters:
time - the time at which a hypothetical command would be sent, in milliseconds since the unix epoch
Returns:
the potential rate average

getOptimalWaitTime

public final long getOptimalWaitTime()
Returns how long one "should" wait before sending a command in this monitor's associated rate class (to avoid being rate limited). This algorithm attempts to stay above the rate limit (or the clear limit, if currently rate limited) plus the error margin. Note that this method will never return a value less than zero.

Returns:
how long one should wait before sending a command in the associated rate class

getTimeUntil

public final long getTimeUntil(long minAvg)
Returns how long one must wait before sending a command in this monitor's associated rate class to keep the current average above the given average. This method ignores the error margin; it returns exactly how long one must wait for the rate average to be equal to or above the given average. Note that this method will never return a value less than zero.

Parameters:
minAvg - the "target" average
Returns:
how long one must wait before sending a command to stay above the given average

getPossibleCmdCount

public final int getPossibleCmdCount()
Returns the number of commands in this rate class that could be sent immediately, without being rate limited.

Returns:
the number of commands in this rate class that could be sent immediately without being rate limited

getMaxCmdCount

public final int getMaxCmdCount()
Returns the maximum number of commands in this rate class that could ever be sent at the same time without being rate-limited. This method is similar to getPossibleCmdCount() but differs in that this method essentially returns the upper limit for the return value of getPossibleCmdCount(): it returns the maximum number of commands that could ever be sent at once; that is, what getPossibleCmdCount() would return if the current rate average were at its maximum.

Returns:
the maximum number of commands that could be sent in this rate class simultaneously without being rate limited