I am trying to implement a code which makes request to external REST endpoint and when that endpoint returns a 404, it should retry for finite times.
The HttpRequest is something like
val responseFuture = Http().singleRequest(HttpRequest(method = requestMethod,
uri = url,
entity = HttpEntity(requestBody).withContentType(ContentTypes.`application/json`)
))
and the response is handled as
responseFuture.onComplete {
case Success(r) =>
if (r.status.isFailure()) Future.failed(new Exception("request failed with status 404"))
else r
case Failure(e) => throw e
}
My retry logic is:
def retryFuture[T](retries: Int, delay: FiniteDuration = 1.second)(fn: => Future[T])(implicit ec: ExecutionContext, s: Scheduler): Future[T] = {
fn.recoverWith {
case _ if retries > 0 => after(delay, s)(retryFuture(retries - 1, delay)(fn))
}
}
The problem is when the endpoint returns 404, it comes as SUCCESS(HttpResponse(404,...) and so the retry is not working. Can anyone point out what can be done to resolve this?