如何在 akka http 中向 POST 请求添加重试?
How to add retries to POST request in akka http?
我正在使用以下 akka http 请求方 api 执行 POST 请求。
Http().singleRequest(....)
我发现 post 的 akka http 不支持重试。
// max-retries = 3 // AKKA http default value is 5, and it doesn't work for POST request
那么在 POST 请求的情况下重试的最佳方法是什么。
你可以试试这样的,基于akka.pattern.retry
object Example {
case class RetryConfig(attempts: Int, minBackoff: FiniteDuration, maxBackoff: FiniteDuration, randomFactor: Double)
class SingleRequestClient(implicit system: ActorSystem) {
def request(request: HttpRequest): Future[HttpResponse] = {
Http().singleRequest(request)
}
}
object SingleRequestClient {
def apply(implicit system: ActorSystem): SingleRequestClient = {
new SingleRequestClient
}
def apply(config: RetryConfig)(implicit system: ActorSystem, ex: ExecutionContext): SingleRequestClient = {
new SingleRequestClient {
override def request(request: HttpRequest): Future[HttpResponse] = {
akka.pattern.retry(
attempt = () => super.request(request),
attempts = config.attempts,
minBackoff = config.minBackoff,
maxBackoff = config.maxBackoff,
randomFactor = config.randomFactor
)(ex, system.scheduler)
}
}
}
}
}
此外,您应该将 max-retries 设置为 0。
Ofc 它只适用于 Future.failed 所以如果你想重试状态 != 200 的请求,你可以稍微改变基本实现
class SingleRequestClient(implicit system: ActorSystem, ex: ExecutionContext) {
def request(request: HttpRequest): Future[HttpResponse] = {
Http().singleRequest(request).map {
case res: HttpResponse if res.status.isSuccess => res
case res: HttpResponse if res.status.isFailure =>
throw new Exception(s"Cannot process request dou to ${res.status.intValue} status")
}
}
}
我正在使用以下 akka http 请求方 api 执行 POST 请求。
Http().singleRequest(....)
我发现 post 的 akka http 不支持重试。
// max-retries = 3 // AKKA http default value is 5, and it doesn't work for POST request
那么在 POST 请求的情况下重试的最佳方法是什么。
你可以试试这样的,基于akka.pattern.retry
object Example {
case class RetryConfig(attempts: Int, minBackoff: FiniteDuration, maxBackoff: FiniteDuration, randomFactor: Double)
class SingleRequestClient(implicit system: ActorSystem) {
def request(request: HttpRequest): Future[HttpResponse] = {
Http().singleRequest(request)
}
}
object SingleRequestClient {
def apply(implicit system: ActorSystem): SingleRequestClient = {
new SingleRequestClient
}
def apply(config: RetryConfig)(implicit system: ActorSystem, ex: ExecutionContext): SingleRequestClient = {
new SingleRequestClient {
override def request(request: HttpRequest): Future[HttpResponse] = {
akka.pattern.retry(
attempt = () => super.request(request),
attempts = config.attempts,
minBackoff = config.minBackoff,
maxBackoff = config.maxBackoff,
randomFactor = config.randomFactor
)(ex, system.scheduler)
}
}
}
}
}
此外,您应该将 max-retries 设置为 0。
Ofc 它只适用于 Future.failed 所以如果你想重试状态 != 200 的请求,你可以稍微改变基本实现
class SingleRequestClient(implicit system: ActorSystem, ex: ExecutionContext) {
def request(request: HttpRequest): Future[HttpResponse] = {
Http().singleRequest(request).map {
case res: HttpResponse if res.status.isSuccess => res
case res: HttpResponse if res.status.isFailure =>
throw new Exception(s"Cannot process request dou to ${res.status.intValue} status")
}
}
}