|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectRateClassInfo
A data structure containing rate limiting information for a specific "class" of SNAC commands.
RateClassInfo
Fields windowSize
10
to
60
)currentAvg
windowSize
commandswarnAvg
5000
ms)limitedAvg
clearAvg
(normally 4000
ms)clearAvg
warnAvg
plus
100
ms, or 5100
msdisconnectAvg
3000
ms)max
6000
ms)RateAck
is sent.
RateInfoCmd
, the current rate for each rate class
within should be set to that rate class's maximum rate
average. Note that a current average is sent,
but should be ignored in the initial RateInfoCmd
.
RateChange
, nothing must be modified (unless the
maximum rate average has been decreased and the current rate is now above
it). You may want to set your current average to the given "current average,"
but this is not advised, as the rate change command may have been sent in
response to a command sent several commands ago (due to network lag). A good
way to do this might be to only set your rate average to the given "current
average" only if the given current average is lower than your client's
computed average. This should be the most conservative and thus reliable way
to handle rate changes.
void computeNewAvg(long lastSent, long oldAvg, RateClassInfo rateClassInfo) { long curTime = System.currentTimeMillis(); long diff = curTime - lastSent; long winSize = rateClassInfo.getWindowSize(); long maxAvg = rateClassInfo.getMax(); currentAvg = ((currentAvg * (winSize - 1)) + diff) / winSize; if (currentAvg > maxAvg) currentAvg = maxAvg; }Using such an algorithm produces results almost exactly consistent with the "current averages" sent in
RateChange
packets, often within a
margin of one or two milliseconds (out of an average of 5000
ms
or lower). (This margin of error is surely due to network traffic and not an
error in the algorithm's above implementation.)
RateChange
with a change code of
RateChange.CODE_LIMITED
. After this happens, all of the commands
sent in the associated rate class will
be ignored by the server until the rate reaches the "clear average", at which point a RateChange
with a change code
of RateChange.CODE_LIMIT_CLEARED may be sent
(though it is not usually sent). Once the average is above the clear average,
however, all is back to normal, as if limiting had never taken place.
Constructor Summary | |
RateClassInfo(int rateClass,
long windowSize,
long clearAvg,
long warnAvg,
long limitedAvg,
long disconnectAvg,
long currentAvg,
long max)
Creates a new rate class information block with the given properties. |
Method Summary | |
long |
getClearAvg()
Returns the rate average above which the user is no longer rate limited. |
CmdType[] |
getCommands()
Returns the commands included in this rate class, or null
if they were not sent (as in a RateChange ). |
long |
getCurrentAvg()
Returns the user's current rate average in this rate class. |
long |
getDisconnectAvg()
Returns the rate average below which the user will be disconnected. |
long |
getLimitedAvg()
Returns the rate average below which the user is rate-limited. |
long |
getMax()
Returns the maximum rate average for this rate class. |
int |
getRateClass()
Returns the ID of the rate class that holds this rate class info. |
long |
getWarnAvg()
Returns the rate average below which the user is "warned." |
long |
getWindowSize()
Returns the "window size" of this rate class. |
long |
getWritableLength()
Returns the length of the data that was or will be written in a call to write . |
static RateClassInfo |
readRateClassInfo(ByteBlock block)
Generates a rate class information block from the given block of data. |
java.lang.String |
toString()
|
void |
write(java.io.OutputStream out)
Writes a representation of this object to the given stream. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public RateClassInfo(int rateClass, long windowSize, long clearAvg, long warnAvg, long limitedAvg, long disconnectAvg, long currentAvg, long max)
rateClass
- the rate class ID that this block describeswindowSize
- the "window size"clearAvg
- the "not rate limited anymore" averagewarnAvg
- the "warned" averagelimitedAvg
- the "rate limited" averagedisconnectAvg
- the "disconnected" averagecurrentAvg
- the current averagemax
- the maximum rate averageMethod Detail |
public static RateClassInfo readRateClassInfo(ByteBlock block)
getTotalSize
method of the returned
RateClassInfo
.
block
- a block of data containing a rate information block
public final int getRateClass()
public final long getWindowSize()
public final long getWarnAvg()
public final long getLimitedAvg()
public final long getClearAvg()
public final long getDisconnectAvg()
public final long getCurrentAvg()
public final long getMax()
public final CmdType[] getCommands()
null
if they were not sent (as in a RateChange
).
public long getWritableLength()
Writable
write
. The value returned by this method must not
change after its first invocation.
getWritableLength
in interface Writable
write
public void write(java.io.OutputStream out) throws java.io.IOException
Writable
getWritableLength
.
write
in interface Writable
out
- the stream to which to write
java.io.IOException
- if an I/O error occurspublic java.lang.String toString()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |