Hi,
I am trying to use dispatch retry package but it seems it does not work with asynchronius handlers onSuccess and onFailure.
Even though retry is defined, output is available after 1st run only.
As per my understanding output whether error or a valid http response must always only be available after all retry attempts are finished OR if success condition returns true
The code snippet is below:
object DispatchTest {
type ResponseEither = Either[Throwable, Response]
type PromiseEither = Promise[Either[Throwable, Response]]
def retry(f: => PromiseEither)
(implicit retryConfig: Retry): PromiseEither = {
val failablePromise = f
Pause(retryConfig.max, retryConfig.delay)(failablePromise){
new Success(retryConfig.buildSuccessCondition())
}
failablePromise
}
case class Retry(
successCodes: List[Int] = List(200),
max: Int = 3,
delay: Duration = Duration(100, TimeUnit.MILLISECONDS)
) {
private[DispatchUtils] def buildSuccessCondition() = (e: ResponseEither) => {
e match {
case Right(r) => println("retry on success ");successCodes.contains(r.getStatusCode())
case Left(t) => println("retry on failure");false
}
}
}
def main(args: Array[String]) {
val http = Http()
val p1 = http(svc)
val p2 = p1.either
implicit val r = Retry()
val p3 = retry(p2)
p3 onSuccess {
case Left(l) => {
println("left of onSuccess")
println(l)
}
case Right(r) => {
println("right of onSuccess")
println(r.getStatusCode())
println(r.getResponseBody())
// println(p1().getStatusCode())
}
}
p3 onFailure {
case e: Any => {
println("onFailure")
println(e)
}
}
}
}
console output >
retry on failure --> only 1 time
left of onSuccess --> no retry and output is available
Could you please let me know how to overcome this?
Thanks,
Sourav