我正在使用 akka http 库为 rest API 编写客户端。该库看起来非常强大,但对我来说它的工作非常不稳定。当我尝试使用 HttpResponse.entity 时,它经常(并非总是)抛出以下异常:
EntityStreamException: Entity stream truncation
在此之后,它完全停止处理后续请求。也许它试图处理一些背压或 Actor 就死了,我不知道。
我如何使用请求级客户端 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
}
或使用连接级客户端 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
}
我是否使用解码器或使用 getDataBytes 手动使用实体并不重要,结果是相同的 - 上述异常(exception)。
响应的 http 状态为 200 OK, header 正常,它是一个“默认”实体(因此,没有分块),内容长度约为 500-2000 Kb(增加 akka.http.parsing.max-content-length 不会没有帮助,尽管默认值应该足够了)。服务器也可以 - 其他平台上的其他 http 库可以很好地使用这个 API。
这是一个错误还是我做错了什么?什么是 scala 最好的非阻塞、异步 http 库?
最佳答案
我对最初的问题有评论,但也在这里回答,所以如果这对任何人有帮助,可以对其进行投票。
这是一个路由路径问题(是的,严重)。我有一个嵌套的路线,它与同一级别的另一条路线以某种方式发生冲突(基本上是奇怪导致一条路线覆盖另一条路线......)。一旦我重新设计了路线的结构,一切都很好。
关于scala - akka http 抛出 EntityStreamException : Entity stream truncation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36216923/