Hi all I have this function: `typealias GetCust...
# arrow
j
Hi all I have this function:
typealias GetCustomer = (config: Config, domain: Domain) -> IO<Either<ApiError, Option<CustomerDetails>>>
which will do a rest call to get CustomerDetails Reading the arrow documentation i managed to get this:
val io = getCustomer(cloudConfig, domain).repeat(IO.concurrent(), Schedule.doWhile(IO.monad()) { it.isEmpty()})
val maybeCustomer = EitherT(io).bind()
How can i add exponential backoff with a max retries of 10 starting with a 1 second delay?
j
Something like this should work: (Probably needs type information, haven't tested this in an editor)
Copy code
Schedule.withMonad(IO.monad()) {
  exponential(1.seconds).whileInput { it.isEmpty() } and recurs(10)
}
This should give you a policy that uses exponential backoff starting with 1 second as long as the input (the result of whatever we are repeating/retrying) holds the predicate. Then we combine with and to force it to stop after 10 retries.
and
combines two schedules and only repeats/retries if both of them would and picks the longer delay of the two (recurs has no delay so it picks exponentials delay) You might also want to consider calling
jittered
to avoid a coordinated retry when the service fails (If there is not a huge amount of traffic this likely won't matter, if there is use
jittered
to slightly randomize the delay of each retry. The bounds are configurable^^)
Oh and I forgot: you probably need to combine the schedule you get from my exaple with
zipRight(identity())
to get the result of the computation^^
This makes the whole example:
Copy code
Schedule.withMonad(IO.monad()) {
  (exponential(1.seconds).whileInput { it.isEmpty() } and recurs(10))
    .jittered(IO.monadDefer())
    .zipRight(identity())
}
👍🏾 1
j
Thanks i will try it