【问题标题】:Multiplex akka streams source into two copies多路复用 akka 流源到两个副本
【发布时间】:2018-11-22 04:07:10
【问题描述】:

我正在尝试将流媒体源缓存到磁盘,同时我还将它作为HttpResponse 发送出去,即我有一个Source[ByteString,_] 我想交给HttpEntity,但我也想将相同的数据运行到 FileIO.toPath 接收器中。

                       |-> FileIO.toPath
Source[ByteString,_] ->|
                       |-> HttpEntity(contentType, Source[ByteString,_]

似乎Broadcast 是我应该用于扇出的,但根据描述,它写入两个接收器,而FileIO.toPath 是一个接收器,HttpEntity 需要一个Source

还有Source.fromGraph,它看起来会从 GraphStage 创建一个源,例如 Broadcast 阶段,但我不太清楚如何在其中获得 FileIO 接收器。

【问题讨论】:

  • 你可以只有一个源 -> FileIO.toPath 的接收器并直接将源传递给 HttyEntity 吗?
  • 从我的实验来看,使用源两次运行源两次,即我下载源两次而不是多路复用单个下载
  • 我认为它无法实现,因为当您的 HttpEntity 将 Source 作为其参数的一部分时,它最终会调用 run() 并且此时您的源将被第二次触发。

标签: scala akka-stream akka-http


【解决方案1】:

你可以使用alsoTo:

val originalSource: Source[ByteString, _] = ???
val cachedSource: Source[ByteString, _] = originalSource.alsoTo(FileIO.toPath(/*...*/))
val entity = HttpEntity(contentType, cachedSource)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多