Keep in akka 流是什么意思?

zer*_*ing 6 scala akka-stream

我正在学习 akka 流,在代码中遇到了 Keep.left 和 Keep.right:

implicit val system = ActorSystem("KafkaProducer")
implicit val materializer = ActorMaterializer()

val source = Source(List("a", "b", "c"))
val sink = Sink.fold[String, String]("")(_ + _)

val runnable: RunnableGraph[Future[String]] = source.toMat(sink)(Keep.right)
val result: Future[String] = runnable.run()
Run Code Online (Sandbox Code Playgroud)

这里的 Keep.right 是什么意思?

Leo*_*o C 7

每个流处理阶段都可以产生一个物化值,可以使用viaMator捕获该值toMat(分别与via()orto()相对)。在您的代码片段中,使用 ofsource.toMat(sink)表示您有兴趣捕获源和接收器的物化值并Keep.right保留物化值的右侧(即接收器)。 Keep.left将保留左侧的物化值(即源),并Keep.both允许您保留两者。

Akka Streams 文档的相关部分提供了更多详细信息。