Akka http -ERROR :Substream Source cannot be materialized more than once If Payload size increased

Akka http -ERROR :Substream Source cannot be materialized more than once If Payload size increased

我正在使用 Akka Http,我将路由定义为

val route = (path(HttpConstants.CreateJob) & post) {
    (entity(as[JobDetailsEntity]) & entity(as[JobEntity])) {
      (jobDetailsEntity: JobDetailsEntity, jobEntity: JobEntity) =>
        val updatedJobEntity = jobEntity.copy(runningSince = DateTime.now().getMillis)
        val updatedJobDetailsEntity = jobDetailsEntity.copy(runningSince = DateTime.now().getMillis).copy(modify_date = DateTime.now().getMillis)
        complete {
          createJobDetails(updatedJobDetailsEntity).map(_.asJson)
          createJob(updatedJobEntity).map(_.asJson)
        }
    }

在这里,我试图在同一个 POST 调用中解组两个实体,当我的 json 有效载荷 ID 较小时,即几个字节然后它工作正常,只要有效载荷大小增加,即大约 10-20 kb 会抛出错误:

Substream Source cannot be materialized more than once

请参阅https://github.com/akka/akka-http/issues/745#issuecomment-271571342

简而言之,如果您需要对您的实体进行两次解组,您应该首先使用 toStrict 以确保整个实体都缓冲在内存中,否则它会被第一次解组过程耗尽而无法用于第二个。

如果实体足够小以至于它适合 akka 的内部缓冲区,那么它碰巧在没有 toStrict 的情况下也能正常工作,那么实际上不涉及耗尽​​。

Note that some directives force an implicit toStrict operation, such as entity(as[String]) and similar ones.

参考 ACKA 文档:Server-Side handling of streaming HTTP Entities