com.twitter.common.util
Class BackoffHelper

java.lang.Object
  extended by com.twitter.common.util.BackoffHelper

public class BackoffHelper
extends Object

A utility for dealing with backoffs of retryable actions.

TODO(John Sirois): investigate synergies with BackoffDecider.


Nested Class Summary
static class BackoffHelper.BackoffStoppedException
          Occurs after the backoff strategy should stop.
 
Constructor Summary
BackoffHelper()
          Creates a new BackoffHelper that uses truncated binary backoff starting at a 1 second backoff and maxing out at a 1 minute backoff.
BackoffHelper(Amount<Long,Time> initialBackoff, Amount<Long,Time> maxBackoff)
          Creates a new BackoffHelper that uses truncated binary backoff starting at the given initialBackoff and maxing out at the given maxBackoff.
BackoffHelper(Amount<Long,Time> initialBackoff, Amount<Long,Time> maxBackoff, boolean stopAtMax)
          Creates a new BackoffHelper that uses truncated binary backoff starting at the given initialBackoff and maxing out at the given maxBackoff.
BackoffHelper(BackoffStrategy backoffStrategy)
          Creates a BackoffHelper that uses the given backoffStrategy to calculate backoffs between retries.
 
Method Summary
<T,E extends Exception>
T
doUntilResult(ExceptionalSupplier<T,E> task)
          Executes the given task using the configured backoff strategy until the task succeeds as indicated by returning a non-null value.
<E extends Exception>
void
doUntilSuccess(ExceptionalSupplier<Boolean,E> task)
          Executes the given task using the configured backoff strategy until the task succeeds as indicated by returning true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BackoffHelper

public BackoffHelper()
Creates a new BackoffHelper that uses truncated binary backoff starting at a 1 second backoff and maxing out at a 1 minute backoff.


BackoffHelper

public BackoffHelper(Amount<Long,Time> initialBackoff,
                     Amount<Long,Time> maxBackoff)
Creates a new BackoffHelper that uses truncated binary backoff starting at the given initialBackoff and maxing out at the given maxBackoff.

Parameters:
initialBackoff - the initial amount of time to back off
maxBackoff - the maximum amount of time to back off

BackoffHelper

public BackoffHelper(Amount<Long,Time> initialBackoff,
                     Amount<Long,Time> maxBackoff,
                     boolean stopAtMax)
Creates a new BackoffHelper that uses truncated binary backoff starting at the given initialBackoff and maxing out at the given maxBackoff. This will either:

Parameters:
initialBackoff - the initial amount of time to back off
maxBackoff - the maximum amount of time to back off
stopAtMax - if true, this will throw BackoffStoppedException when the max backoff is reached

BackoffHelper

public BackoffHelper(BackoffStrategy backoffStrategy)
Creates a BackoffHelper that uses the given backoffStrategy to calculate backoffs between retries.

Parameters:
backoffStrategy - the backoff strategy to use
Method Detail

doUntilSuccess

public <E extends Exception> void doUntilSuccess(ExceptionalSupplier<Boolean,E> task)
                    throws InterruptedException,
                           BackoffHelper.BackoffStoppedException,
                           E extends Exception
Executes the given task using the configured backoff strategy until the task succeeds as indicated by returning true.

Parameters:
task - the retryable task to execute until success
Throws:
InterruptedException - if interrupted while waiting for the task to execute successfully
BackoffHelper.BackoffStoppedException - if the backoff stopped unsuccessfully
E - if the task throws
E extends Exception

doUntilResult

public <T,E extends Exception> T doUntilResult(ExceptionalSupplier<T,E> task)
                throws InterruptedException,
                       BackoffHelper.BackoffStoppedException,
                       E extends Exception
Executes the given task using the configured backoff strategy until the task succeeds as indicated by returning a non-null value.

Parameters:
task - the retryable task to execute until success
Returns:
the result of the successfully executed task
Throws:
InterruptedException - if interrupted while waiting for the task to execute successfully
BackoffHelper.BackoffStoppedException - if the backoff stopped unsuccessfully
E - if the task throws
E extends Exception