-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scheduled and repeat API + retry refactoring (#172)
- Loading branch information
Showing
22 changed files
with
1,009 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,63 @@ | ||
package ox.resilience | ||
|
||
import ox.{EitherMode, ErrorMode, sleep} | ||
import ox.{EitherMode, ErrorMode} | ||
import ox.scheduling.* | ||
|
||
import scala.annotation.tailrec | ||
import scala.concurrent.duration.* | ||
import scala.util.Try | ||
|
||
/** Retries an operation returning a direct result until it succeeds or the policy decides to stop. | ||
/** Retries an operation returning a direct result until it succeeds or the config decides to stop. | ||
* | ||
* @param policy | ||
* The retry policy - see [[RetryPolicy]]. | ||
* [[retry]] is a special case of [[scheduled]] with a given set of defaults. See [[RetryConfig]] for more details. | ||
* | ||
* @param config | ||
* The retry config - see [[RetryConfig]]. | ||
* @param operation | ||
* The operation to retry. | ||
* @return | ||
* The result of the function if it eventually succeeds. | ||
* @throws anything | ||
* The exception thrown by the last attempt if the policy decides to stop. | ||
* The exception thrown by the last attempt if the config decides to stop. | ||
* @see | ||
* [[scheduled]] | ||
*/ | ||
def retry[T](policy: RetryPolicy[Throwable, T])(operation: => T): T = | ||
retryEither(policy)(Try(operation).toEither).fold(throw _, identity) | ||
def retry[T](config: RetryConfig[Throwable, T])(operation: => T): T = | ||
retryEither(config)(Try(operation).toEither).fold(throw _, identity) | ||
|
||
/** Retries an operation returning an [[scala.util.Either]] until it succeeds or the policy decides to stop. Note that any exceptions thrown | ||
/** Retries an operation returning an [[scala.util.Either]] until it succeeds or the config decides to stop. Note that any exceptions thrown | ||
* by the operation aren't caught and don't cause a retry to happen. | ||
* | ||
* @param policy | ||
* The retry policy - see [[RetryPolicy]]. | ||
* [[retryEither]] is a special case of [[scheduledEither]] with a given set of defaults. See [[RetryConfig]] for more details. | ||
* | ||
* @param config | ||
* The retry config - see [[RetryConfig]]. | ||
* @param operation | ||
* The operation to retry. | ||
* @return | ||
* A [[scala.util.Right]] if the function eventually succeeds, or, otherwise, a [[scala.util.Left]] with the error from the last attempt. | ||
* @see | ||
* [[scheduledEither]] | ||
*/ | ||
def retryEither[E, T](policy: RetryPolicy[E, T])(operation: => Either[E, T]): Either[E, T] = | ||
retryWithErrorMode(EitherMode[E])(policy)(operation) | ||
def retryEither[E, T](config: RetryConfig[E, T])(operation: => Either[E, T]): Either[E, T] = | ||
retryWithErrorMode(EitherMode[E])(config)(operation) | ||
|
||
/** Retries an operation using the given error mode until it succeeds or the policy decides to stop. Note that any exceptions thrown by the | ||
/** Retries an operation using the given error mode until it succeeds or the config decides to stop. Note that any exceptions thrown by the | ||
* operation aren't caught (unless the operation catches them as part of its implementation) and don't cause a retry to happen. | ||
* | ||
* [[retryWithErrorMode]] is a special case of [[scheduledWithErrorMode]] with a given set of defaults. See [[RetryConfig]] for more | ||
* details. | ||
* | ||
* @param em | ||
* The error mode to use, which specifies when a result value is considered success, and when a failure. | ||
* @param policy | ||
* The retry policy - see [[RetryPolicy]]. | ||
* @param config | ||
* The retry config - see [[RetryConfig]]. | ||
* @param operation | ||
* The operation to retry. | ||
* @return | ||
* Either: | ||
* - the result of the function if it eventually succeeds, in the context of `F`, as dictated by the error mode. | ||
* - the error `E` in context `F` as returned by the last attempt if the policy decides to stop. | ||
* - the error `E` in context `F` as returned by the last attempt if the config decides to stop. | ||
* @see | ||
* [[scheduledWithErrorMode]] | ||
*/ | ||
def retryWithErrorMode[E, F[_], T](em: ErrorMode[E, F])(policy: RetryPolicy[E, T])(operation: => F[T]): F[T] = | ||
@tailrec | ||
def loop(attempt: Int, remainingAttempts: Option[Int], lastDelay: Option[FiniteDuration]): F[T] = | ||
def sleepIfNeeded = | ||
val delay = policy.schedule.nextDelay(attempt, lastDelay) | ||
if delay.toMillis > 0 then sleep(delay) | ||
delay | ||
|
||
operation match | ||
case v if em.isError(v) => | ||
val error = em.getError(v) | ||
policy.onRetry(attempt, Left(error)) | ||
|
||
if policy.resultPolicy.isWorthRetrying(error) && remainingAttempts.forall(_ > 0) then | ||
val delay = sleepIfNeeded | ||
loop(attempt + 1, remainingAttempts.map(_ - 1), Some(delay)) | ||
else v | ||
case v => | ||
val result = em.getT(v) | ||
policy.onRetry(attempt, Right(result)) | ||
|
||
if !policy.resultPolicy.isSuccess(result) && remainingAttempts.forall(_ > 0) then | ||
val delay = sleepIfNeeded | ||
loop(attempt + 1, remainingAttempts.map(_ - 1), Some(delay)) | ||
else v | ||
|
||
val remainingAttempts = policy.schedule match | ||
case finiteSchedule: Schedule.Finite => Some(finiteSchedule.maxRetries) | ||
case _ => None | ||
|
||
loop(1, remainingAttempts, None) | ||
def retryWithErrorMode[E, F[_], T](em: ErrorMode[E, F])(config: RetryConfig[E, T])(operation: => F[T]): F[T] = | ||
scheduledWithErrorMode(em)(config.toScheduledConfig)(operation) |
2 changes: 1 addition & 1 deletion
2
...src/main/scala/ox/resilience/Jitter.scala → ...src/main/scala/ox/scheduling/Jitter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.