-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scheduled and repeat API + retry refactoring #172
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
f5a0581
refactor retry* functions to scheduled*
micossow 1c14d0e
refactor Schedule.Every -> FixedRate
micossow c449e30
refactor schedule -> runScheduled and add repeat*
micossow f526f74
fix compilation error
micossow c27ed5d
fix compilation error and warnings
micossow 3728c62
fix doc compile
micossow d511f02
add Schedule.InitialDelay, refactor Exponential.initialDelay -> first…
micossow 373ee38
Schedule doc cleanup and disambiguation
micossow c68a9d6
add todo
micossow 686e152
remove Schedule.FixedRate and move the start-to-start concept to runS…
micossow bfa4da0
fix default delayPolicy for retry
micossow a3e71b3
add tolerance to fixedRate tests
micossow cc73acc
reduce sleep time in fixedRate tests
micossow d0d94b0
refactor runScheduled -> scheduled
micossow 6406eff
refactor RetryPolicy -> RetryConfig
micossow 013bbdf
refactor Schedule.fallbackTo -> andThen
micossow e1a5805
close todo
micossow 3ba7005
fix compileDocumentation
micossow 90d2164
restore private modifier to Exponential.delay
micossow 8c3128e
[doc] replace policy with config where applicable
micossow 3569463
[doc] repeat and RepeatConfig doc
micossow 8834a18
add initialDelay to RepeatConfig
micossow 60e51b6
another Schedule refactoring
micossow 5c6ef40
refactor DelayPolicy -> SleepMode
micossow 88e9cec
fix initialDelay combined with forever schedule
micossow 739f1a7
add ImmediateRepeatTest
micossow 3715c25
add adr
micossow 732c37e
update docs
micossow e9d7660
fix accidentally modified generated doc
micossow e716e77
reformat adr
micossow 809275d
fix doc
micossow 8f9cbc7
refactor attempt -> iteration in Schedule
micossow c966a90
remove shouldContinueOnError from repeat
micossow a893508
[doc] update repeat doc about combination with retry
micossow 657534a
refactor variable/parameter names in scheduled
micossow 0483281
document to prefer to use repeat, retry instead of scheduled
micossow 6bfb177
[doc] mention how Retry/RetryConfig are special case of ScheduledConfig
micossow 493888f
scheduled: rename iteration -> invocation
micossow a25f23e
fix doc compile
micossow 5b3290c
combining Schedules refactoring and examples
micossow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd also mention here that
retry
is a special-case ofschedule
, with a given set of defaults (and enumarte these defaults). Maybe this should be in the@see
section?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done