Package dev.langchain4j.internal
Class RetryUtils.RetryPolicy
java.lang.Object
dev.langchain4j.internal.RetryUtils.RetryPolicy
- Enclosing class:
RetryUtils
This class encapsulates a retry policy.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
This class encapsulates a retry policy builder. -
Constructor Summary
ConstructorsConstructorDescriptionRetryPolicy
(int maxRetries, int delayMillis, double jitterScale, double backoffExp) Construct a RetryPolicy. -
Method Summary
Modifier and TypeMethodDescriptionint
jitterDelayMillis
(int retry) This method returns the jitter delay in milliseconds after a given retry.double
rawDelayMs
(int retry) This method returns the raw delay in milliseconds after a given retry.void
sleep
(int retry) This method sleeps after a given retry.<T> T
This method attempts to execute a given action up to 3 times with an exponential backoff.<T> T
This method attempts to execute a given action up to a specified number of times with an exponential backoff.
-
Constructor Details
-
RetryPolicy
public RetryPolicy(int maxRetries, int delayMillis, double jitterScale, double backoffExp) Construct a RetryPolicy.- Parameters:
maxRetries
- The maximum number of retries. The action can be executed up tomaxRetries + 1
times.delayMillis
- The delay in milliseconds.jitterScale
- The jitter scale.backoffExp
- The backoff exponent.
-
-
Method Details
-
rawDelayMs
public double rawDelayMs(int retry) This method returns the raw delay in milliseconds after a given retry.- Parameters:
retry
- The retry number.- Returns:
- The raw delay in milliseconds.
-
jitterDelayMillis
public int jitterDelayMillis(int retry) This method returns the jitter delay in milliseconds after a given retry.- Parameters:
retry
- The retry number.- Returns:
- The jitter delay in milliseconds.
-
sleep
public void sleep(int retry) This method sleeps after a given retry.- Parameters:
retry
- The retry number.
-
withRetry
This method attempts to execute a given action up to 3 times with an exponential backoff. If the action fails on all attempts, it throws a RuntimeException.- Type Parameters:
T
- The type of the result of the action.- Parameters:
action
- The action to be executed.- Returns:
- The result of the action if it is successful.
- Throws:
RuntimeException
- if the action fails on all attempts.
-
withRetry
This method attempts to execute a given action up to a specified number of times with an exponential backoff. If the action fails on all attempts, it throws a RuntimeException.- Type Parameters:
T
- The type of the result of the action.- Parameters:
action
- The action to be executed.maxRetries
- The maximum number of retries. The action can be executed up tomaxRetries + 1
times.- Returns:
- The result of the action if it is successful.
- Throws:
RuntimeException
- if the action fails on all attempts.
-