Packagecom.threerings.util
Classpublic class Throttle

A throttle is used to prevent code from attempting a particular operation too often. Often it is desirable to retry an operation under failure conditions, but simplistic approaches to retrying operations can lead to large numbers of spurious attempts to do something that will obviously fail. The throttle class provides a mechanism for limiting such attempts by measuring whether or not an activity has been performed N times in the last M seconds. The user of the class decides the appropriate throttle parameters and then simply calls through to throttle to determine whether or not to go ahead with the operation.

For example:

 protected Throttle _throttle = new Throttle(5, 60000L);
  public void performOp ()
     throws UnavailableException
 {
     if (_throttle.throttleOp()) {
         throw new UnavailableException();
     }
      // perform operation
 }
 



Protected Properties
 PropertyDefined by
  _oldestOp : int
Throttle
  _ops : Array
Throttle
  _period : int
Throttle
Public Methods
 MethodDefined by
  
Throttle(operations:int, period:int)
Constructs a new throttle instance that will allow the specified number of operations to proceed within the specified period (the period is measured in milliseconds).
Throttle
  
Returns the timestamp of the most recently recorded operation.
Throttle
  
noteOp(timeStamp:int):void
Note that an operation occurred at the specified timestamp.
Throttle
  
opsToString():String
For debugging, includes the age of all operations.
Throttle
  
reinit(operations:int, period:int):void
Updates the number of operations for this throttle to a new maximum, retaining the current history of operations if the limit is being increased and truncating the newest operations if the limit is decreased.
Throttle
  
throttleOp():Boolean
Registers an attempt at an operation and returns false if the operation should be performed or true if it should be throttled (meaning N operations have already been performed in the last M seconds).
Throttle
  
throttleOpAt(timeStamp:int):Boolean
Registers an attempt at an operation and returns false if the operation should be performed or true if it should be throttled (meaning N operations have already been performed in the last M seconds).
Throttle
  
toString():String
Throttle
  
wouldThrottle(timeStamp:int):Boolean
Check to see if we would throttle an operation occuring at the specified timestamp.
Throttle
Property detail
_oldestOpproperty
protected var _oldestOp:int
_opsproperty 
protected var _ops:Array
_periodproperty 
protected var _period:int
Constructor detail
Throttle()constructor
public function Throttle(operations:int, period:int)

Constructs a new throttle instance that will allow the specified number of operations to proceed within the specified period (the period is measured in milliseconds).

As operations and period define a ratio, use the smallest value possible for operations as an array is created to track the time at which each operation was performed (e.g. use 6 ops per 10 seconds rather than 60 ops per 100 seconds if possible). However, note that you may not always want to reduce the ratio as much as possible if you wish to allow bursts of operations up to some large value. Parameters

operations:int
 
period:int
Method detail
getLatestOperation()method
public function getLatestOperation():int

Returns the timestamp of the most recently recorded operation.

Returns
int
noteOp()method 
public function noteOp(timeStamp:int):void

Note that an operation occurred at the specified timestamp. This method should be used with {

Parameters
timeStamp:int
opsToString()method 
public function opsToString():String

For debugging, includes the age of all operations.

Returns
String
reinit()method 
public function reinit(operations:int, period:int):void

Updates the number of operations for this throttle to a new maximum, retaining the current history of operations if the limit is being increased and truncating the newest operations if the limit is decreased.

Parameters
operations:int — the new maximum number of operations.
 
period:int — the new period (in milliseconds).
throttleOp()method 
public function throttleOp():Boolean

Registers an attempt at an operation and returns false if the operation should be performed or true if it should be throttled (meaning N operations have already been performed in the last M seconds).

Returns
Boolean — true if the throttle is activated, false if the operation can proceed.
throttleOpAt()method 
public function throttleOpAt(timeStamp:int):Boolean

Registers an attempt at an operation and returns false if the operation should be performed or true if it should be throttled (meaning N operations have already been performed in the last M seconds).

Parameters
timeStamp:int — the timestamp at which this operation is being attempted.

Returns
Boolean — true if the throttle is activated, false if the operation can proceed.
toString()method 
public function toString():String

Returns
String
wouldThrottle()method 
public function wouldThrottle(timeStamp:int):Boolean

Check to see if we would throttle an operation occuring at the specified timestamp. Typically used in conjunction with {

Parameters
timeStamp:int

Returns
Boolean