我正在尝试使用Akka创建具有重试策略的Http客户端,在该策略中,我希望最大重试次数,然后返回错误。有此代码
private def makeRequest[T](system: ActorSystem,
host: String,
unmarshallFunc: ResponseEntity => Future[Either[ServiceNowAccessException, T]]): Future[Either[ServiceNowAccessException, T]] = {
implicit val mat: Materializer = Materializer(system)
RestartSource.withBackoff(
minBackoff = 10 seconds,
maxBackoff = 10 seconds,
randomFactor = 0.2,
maxRestarts = 2
) { () =>
Source.future(Http()(system).singleRequest(HttpRequest(uri = host)))
.mapAsync(parallelism = 1) {
case HttpResponse(OK, _, entity, _) => unmarshallFunc(entity)
case HttpResponse(InternalServerError, _, _, _) =>
Future(Left(ServiceNowAccessException(InternalServerError.intValue)))
case HttpResponse(statusCode, _, _, _) =>
Future(Left(ServiceNowAccessException(statusCode.intValue())))
}
}.runWith(Sink.head)
}
maxRestarts
之后,我无法强制执行该错误,并且它一直试图永久连接。
任何想法我的代码有什么问题
致谢。