Skip to content

Commit

Permalink
Simplify ScheduleContinue
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 31, 2024
1 parent 1f48726 commit 05136b1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/ox/resilience/AdaptiveRetry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ case class AdaptiveRetry(
attempt match
case Left(value) =>
// If we want to retry we try to acquire tokens from bucket
if config.resultPolicy.isWorthRetrying(value) then ScheduleContinue.fromBool(tokenBucket.tryAcquire(failureCost))
if config.resultPolicy.isWorthRetrying(value) then ScheduleContinue(tokenBucket.tryAcquire(failureCost))
else ScheduleContinue.No
case Right(value) =>
// If we are successful, we release tokens to bucket and end schedule
if config.resultPolicy.isSuccess(value) then
tokenBucket.release(successReward)
ScheduleContinue.No
// If it is not success we check if we need to acquire tokens, then we check bucket, otherwise we continue
else if shouldPayPenaltyCost(value) then ScheduleContinue.fromBool(tokenBucket.tryAcquire(failureCost))
else if shouldPayPenaltyCost(value) then ScheduleContinue(tokenBucket.tryAcquire(failureCost))
else ScheduleContinue.Yes
end match
end afterAttempt
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/ox/resilience/RetryConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ case class RetryConfig[E, T](
val afterAttempt: (Int, Either[E, T]) => ScheduleContinue = (attemptNum, attempt) =>
onRetry(attemptNum, attempt)
attempt match
case Left(value) => ScheduleContinue.fromBool(resultPolicy.isWorthRetrying(value))
case Right(value) => ScheduleContinue.fromBool(!resultPolicy.isSuccess(value))
case Left(value) => ScheduleContinue(resultPolicy.isWorthRetrying(value))
case Right(value) => ScheduleContinue(!resultPolicy.isSuccess(value))
end afterAttempt

ScheduledConfig(schedule, afterAttempt, SleepMode.Delay)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/ox/scheduling/RepeatConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ case class RepeatConfig[E, T](
val afterAttempt: (Int, Either[E, T]) => ScheduleContinue = (_, attempt) =>
attempt match
case Left(_) => ScheduleContinue.No
case Right(value) => ScheduleContinue.fromBool(shouldContinueOnResult(value))
case Right(value) => ScheduleContinue(shouldContinueOnResult(value))
end afterAttempt

ScheduledConfig(schedule, afterAttempt, SleepMode.Interval)
Expand Down
15 changes: 5 additions & 10 deletions core/src/main/scala/ox/scheduling/scheduled.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,26 @@ enum SleepMode:
*/
case Interval

/** Delay (since the end of the last operation), i.e. sleeps the duration provided by the schedule before the next operation starts.
*/
/** Delay (since the end of the last operation), i.e. sleeps the duration provided by the schedule before the next operation starts. */
case Delay
end SleepMode

/** @see [[ScheduleConfig.afterAttempt]] */
enum ScheduleContinue(val continue: Boolean):
case Yes extends ScheduleContinue(true)
case No extends ScheduleContinue(false)

end ScheduleContinue

object ScheduleContinue:
def fromBool(predicate: Boolean): ScheduleContinue =
if predicate then Yes
else No
end ScheduleContinue
def apply(predicate: Boolean): ScheduleContinue = if predicate then Yes else No

/** A config that defines how to schedule an operation.
*
* @param schedule
* The schedule which determines the maximum number of invocations and the duration between subsequent invocations. See [[Schedule]] for
* more details.
* @param afterAttempt
* A function that determines if schedule should continue. It is invoked after every attempt with current invocations number (starting
* from 1) and the result of an operation. It can contain side effects.
* A function that determines if schedule should continue. It is invoked after every attempt with current invocation number (starting
* from 1) and the result of the operation.
* @param sleepMode
* The mode that specifies how to interpret the duration provided by the schedule. See [[SleepMode]] for more details.
* @tparam E
Expand Down

0 comments on commit 05136b1

Please sign in to comment.