Class Polling
- java.lang.Object
-
- ai.extend.wrapper.utilities.polling.Polling
-
public final class Polling extends java.lang.ObjectUtility class for polling operations with hybrid polling strategy.This class provides methods to poll an API endpoint until a terminal condition is met. The default strategy uses fast polling at fixed intervals for an initial period, then switches to exponential backoff with jitter.
Default behavior:
- Fast phase: Poll every 1 second for the first 30 seconds
- Backoff phase: Exponential backoff with 1.15x multiplier, max 30 second delay
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intcalculateBackoffDelay(int attempt, int initialDelayMs, int maxDelayMs, double jitterFraction)Calculates the next delay using exponential backoff with default 2x multiplier.static intcalculateBackoffDelay(int attempt, int initialDelayMs, int maxDelayMs, double jitterFraction, double backoffMultiplier)Calculates the next delay using exponential backoff with proportional jitter.static intcalculateHybridDelay(long elapsedMs, int fastPollDurationMs, int fastPollIntervalMs, int initialDelayMs, int maxDelayMs, double backoffMultiplier, double jitterFraction)Calculates the delay for a hybrid polling strategy based on elapsed time.static <T> TpollUntilDone(java.util.function.Supplier<T> retrieve, java.util.function.Predicate<T> isTerminal)Polls a retrieve function until a terminal condition is met, using default options.static <T> TpollUntilDone(java.util.function.Supplier<T> retrieve, java.util.function.Predicate<T> isTerminal, PollingOptions options)Polls a retrieve function until a terminal condition is met.
-
-
-
Method Detail
-
calculateBackoffDelay
public static int calculateBackoffDelay(int attempt, int initialDelayMs, int maxDelayMs, double jitterFraction, double backoffMultiplier)Calculates the next delay using exponential backoff with proportional jitter.Formula: min(initialDelay * multiplier^attempt, maxDelay) * (1 + random(-jitter, +jitter))
- Parameters:
attempt- The current attempt number (0-based)initialDelayMs- The initial delay in millisecondsmaxDelayMs- The maximum delay in millisecondsjitterFraction- The jitter fraction (e.g., 0.25 for ±25%)backoffMultiplier- The multiplier for exponential backoff (default: 2.0)- Returns:
- The calculated delay in milliseconds
-
calculateBackoffDelay
public static int calculateBackoffDelay(int attempt, int initialDelayMs, int maxDelayMs, double jitterFraction)Calculates the next delay using exponential backoff with default 2x multiplier.- Parameters:
attempt- The current attempt number (0-based)initialDelayMs- The initial delay in millisecondsmaxDelayMs- The maximum delay in millisecondsjitterFraction- The jitter fraction (e.g., 0.25 for ±25%)- Returns:
- The calculated delay in milliseconds
-
calculateHybridDelay
public static int calculateHybridDelay(long elapsedMs, int fastPollDurationMs, int fastPollIntervalMs, int initialDelayMs, int maxDelayMs, double backoffMultiplier, double jitterFraction)Calculates the delay for a hybrid polling strategy based on elapsed time.During the fast polling phase (elapsed < fastPollDurationMs), returns a fixed interval with jitter. After the fast phase ends, switches to exponential backoff.
- Parameters:
elapsedMs- Total elapsed time since polling startedfastPollDurationMs- Duration of fast polling phase in millisecondsfastPollIntervalMs- Interval between polls during fast phase in millisecondsinitialDelayMs- Initial delay for backoff phase in millisecondsmaxDelayMs- Maximum delay cap in millisecondsbackoffMultiplier- Multiplier for exponential backoffjitterFraction- Jitter fraction for randomization- Returns:
- The delay in milliseconds until the next poll
-
pollUntilDone
public static <T> T pollUntilDone(java.util.function.Supplier<T> retrieve, java.util.function.Predicate<T> isTerminal, PollingOptions options) throws PollingTimeoutErrorPolls a retrieve function until a terminal condition is met.This method uses a hybrid polling strategy: fast polling at fixed intervals for an initial period, then exponential backoff with jitter. This provides low latency for quick operations while still reducing server load for longer ones.
Default behavior:
- Fast phase: Poll every 1 second for the first 30 seconds
- Backoff phase: Exponential backoff with 1.15x multiplier, max 30 second delay
- Type Parameters:
T- The type of result returned by the retrieve function- Parameters:
retrieve- A supplier that retrieves the current stateisTerminal- A predicate that returns true when polling should stopoptions- Polling configuration options- Returns:
- The final result when a terminal state is reached
- Throws:
PollingTimeoutError- if maxWaitMs is set and exceeded
-
pollUntilDone
public static <T> T pollUntilDone(java.util.function.Supplier<T> retrieve, java.util.function.Predicate<T> isTerminal)Polls a retrieve function until a terminal condition is met, using default options.Polls indefinitely until a terminal state is reached using hybrid polling strategy.
- Type Parameters:
T- The type of result returned by the retrieve function- Parameters:
retrieve- A supplier that retrieves the current stateisTerminal- A predicate that returns true when polling should stop- Returns:
- The final result when a terminal state is reached
-
-