我正在使用akka http库为rest API编写客户端.该库似乎非常强大,但它对我来说非常不稳定.当我尝试使用HttpResponse.entity时,它经常(并非总是)抛出以下异常:
EntityStreamException:实体流截断
然后它停止处理后续请求.也许它试图处理一些背压或演员只是死,我不知道.
使用请求级客户端API发送请求无关紧要:
def pollSearchResult(searchId: String): FutureActionResult[String, SearchResult] = {
val request = HttpRequest(GET, s"http://***/Search/$searchId")
for {
response <- Http().singleRequest(request)
entity <- Unmarshal(response.entity).to[String]
result = entity.decodeEither[SearchResult]
} yield result
}
Run Code Online (Sandbox Code Playgroud)
或使用连接级客户端API:
val client = Http(actorSystem).outgoingConnection("***")
def pollSearchResult(searchId: String): FutureActionResult[String, SearchResult] = {
val request = HttpRequest(GET, s"Search/$searchId")
for {
response <- Source.single(request).via(client).runWith(Sink.head)
entity <- Unmarshal(response.entity).to[String]
result = entity.decodeEither[SearchResult]
} yield result
}
Run Code Online (Sandbox Code Playgroud)
无论是使用unmarshaller还是使用getDataBytes手动使用实体,都不会遇到问题,结果是相同的 - 上述异常.
响应的http状态为200 OK,标题正常,它是"默认"实体(因此,没有分块),内容长度约为500-2000 Kb(增加akka.http.parsing.max-content-length doesn没有帮助,虽然默认值应该足够了).服务器也没问题 - 其他平台上的其他http库也可以使用这个API.
这是一个错误还是我做错了什么?什么是scala最好的非阻塞,asycnhonous http库?