com.twitter.common.util
Class TruncatedBinaryBackoff

java.lang.Object
  extended by com.twitter.common.util.TruncatedBinaryBackoff
All Implemented Interfaces:
BackoffStrategy

public class TruncatedBinaryBackoff
extends Object
implements BackoffStrategy

A BackoffStrategy that implements truncated binary exponential backoff.


Constructor Summary
TruncatedBinaryBackoff(Amount<Long,Time> initialBackoff, Amount<Long,Time> maxBackoff)
          Same as main constructor, but this will always return true from shouldContinue().
TruncatedBinaryBackoff(Amount<Long,Time> initialBackoff, Amount<Long,Time> maxBackoff, boolean stopAtMax)
          Creates a new TruncatedBinaryBackoff that will start by backing off for initialBackoff and then backoff of twice as long each time its called until reaching the maxBackoff at which point shouldContinue() will return false and any future backoffs will always wait for that amount of time.
 
Method Summary
 long calculateBackoffMs(long lastBackoffMs)
          Calculates the amount of time to backoff from an operation.
 boolean shouldContinue()
          Returns whether to continue backing off.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TruncatedBinaryBackoff

public TruncatedBinaryBackoff(Amount<Long,Time> initialBackoff,
                              Amount<Long,Time> maxBackoff,
                              boolean stopAtMax)
Creates a new TruncatedBinaryBackoff that will start by backing off for initialBackoff and then backoff of twice as long each time its called until reaching the maxBackoff at which point shouldContinue() will return false and any future backoffs will always wait for that amount of time.

Parameters:
initialBackoff - the intial amount of time to backoff
maxBackoff - the maximum amount of time to backoff
stopAtMax - whether shouldContinue() returns false when the max is reached

TruncatedBinaryBackoff

public TruncatedBinaryBackoff(Amount<Long,Time> initialBackoff,
                              Amount<Long,Time> maxBackoff)
Same as main constructor, but this will always return true from shouldContinue().

Parameters:
initialBackoff - the intial amount of time to backoff
maxBackoff - the maximum amount of time to backoff
Method Detail

calculateBackoffMs

public long calculateBackoffMs(long lastBackoffMs)
Description copied from interface: BackoffStrategy
Calculates the amount of time to backoff from an operation.

Specified by:
calculateBackoffMs in interface BackoffStrategy
Parameters:
lastBackoffMs - the last used backoff in milliseconds where 0 signifies no backoff has been performed yet
Returns:
the amount of time in milliseconds to back off before retrying the operation

shouldContinue

public boolean shouldContinue()
Description copied from interface: BackoffStrategy
Returns whether to continue backing off.

Specified by:
shouldContinue in interface BackoffStrategy
Returns:
whether to continue backing off