Interface RetryBackoffContext

All Known Implementing Classes:
RetryBackoffContext.Exponential

public interface RetryBackoffContext
Interface to keep the context required to execute a backoff strategy for retries.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    Exception class to signal deadline was exceeded while attempting retries with a retry strategy.
    static class 
    Implementation based on an exponential backoff.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Return the time in milliseconds to wait to the next retry.
    boolean
    Check if we have exceeded our time deadline to retry.
    long
    Return the elapsed between the last time to deadlineExceeded() and the last call to start(long).
    exponential(String propertyPrefix, long defaultInitialDelayMillis, long defaultMaxDelayMillis, double defaultBackoffFactor, double defaultBackoffJitter)
    Create an exponential backoff RetryBackoffContext.
    void
    Inform this context object that a retry without applying backoff will be performed.
    void
    Reset this object to a state where no errors have occurred, and it has not been started.
    int
    Number of retries performed since last started.
    void
    start(long timeOfDeadlineMillis)
    Start tracking time to execute a backoff retry strategy using this context object.
    void
    startIfNotAlreadyStarted(long timeOfDeadlineMillis)
    If not already started, start tracking time to execute a backoff retry strategy using this context object exactly like start(long), including defining a deadline.
    long
    Absolute point in time when the last call to startMillis() was invoked.
    long
    Returns the absolute epoch time in milliseconds for the deadline.
  • Method Details

    • start

      void start(long timeOfDeadlineMillis)
      Start tracking time to execute a backoff retry strategy using this context object. A deadline is defined when this method is called.
      Parameters:
      timeOfDeadlineMillis - absolute time as milliseconds from the epoch to the deadline.
    • startIfNotAlreadyStarted

      void startIfNotAlreadyStarted(long timeOfDeadlineMillis)
      If not already started, start tracking time to execute a backoff retry strategy using this context object exactly like start(long), including defining a deadline. If already started, this call does nothing.
      Parameters:
      timeOfDeadlineMillis - absolute time as milliseconds from the epoch to the deadline.
    • reset

      void reset()
      Reset this object to a state where no errors have occurred, and it has not been started.
    • retryCount

      int retryCount()
      Number of retries performed since last started. Useful for clients that want to log a retry summary after deadline exceeded.
      Returns:
      number of retries performed.
    • startMillis

      long startMillis()
      Absolute point in time when the last call to startMillis() was invoked.
      Returns:
      a milliseconds from the epoch timestamp.
    • deadlineExceeded

      boolean deadlineExceeded()
      Check if we have exceeded our time deadline to retry.
      Returns:
      true if the retry deadline has been exceeded, false otherwise.
    • noBackoff

      void noBackoff()
      Inform this context object that a retry without applying backoff will be performed.

      If the caller has some criteria to decide to perform retries in some cases without applying a backoff (eg, if a failure due to authentication requires to renew credentials, which makes sense to be done right away since we understand the reason for the failure), the calling this function lets this object keep the right context for the number of retries performed.

      If this method is called before a call to start(long) has been made, it will be equivalent to having made a call to start tracking time with an infinite deadline before the call.

    • backoffMillis

      long backoffMillis()
      Return the time in milliseconds to wait to the next retry. It is assumed the caller will perform a retry at that future point in time. Thread.sleep(long).

      If this method is called before a call to start(long) has been made, it will be equivalent to having made a call to start tracking time with an infinite deadline before the call, and the initial backoff returned would be zero; subsequent calls would return the initial backoff, and exponentially generated backoffs (as normal) from there.

      Returns:
      time in milliseconds to wait until the next retry.
    • elapsedMillis

      long elapsedMillis()
      Return the elapsed between the last time to deadlineExceeded() and the last call to start(long).
      Returns:
      elapsed time in milliseconds.
    • timeOfDeadlineMillis

      long timeOfDeadlineMillis()
      Returns the absolute epoch time in milliseconds for the deadline. Note this method only returns a valid deadline if start has already been called on this Context.
      Returns:
      epoch time of deadline in milliseconds.
    • exponential

      static RetryBackoffContext.Exponential exponential(String propertyPrefix, long defaultInitialDelayMillis, long defaultMaxDelayMillis, double defaultBackoffFactor, double defaultBackoffJitter)
      Create an exponential backoff RetryBackoffContext. See Exponential(long, long, double, double) for explanation of the parameters.
      Parameters:
      propertyPrefix - The prefix to use for the properties
      defaultInitialDelayMillis - The default initial delay for retries
      defaultMaxDelayMillis - The default max delay for retries
      defaultBackoffFactor - The default backoff factor for retries
      defaultBackoffJitter - The default backoff jitter for retries
      Returns:
      a populated RetryBackoffContext.Exponential backoff context.