阿卡流。结合物化值。需要映射期货。使用哪个 ExecutionContext/Dispatcher?

Akka-stream. Combining materialized-values. Need to map futures. Which ExecutionContext/Dispatcher to use?

给定:

求职: AtoC: GraphStageWithMaterializedValue[A, C, Future[AtoCRuntimeApi]].

在我的特殊情况下,根据 AtoBRuntimeApiBtoARuntimeApi 实施 AtoCRuntimeApi 真的很方便。

所以我想将 AtoCRuntimeApi 定义为 case class AtoCRuntimeApi(a2b: AtoBRuntimeApi, b2c: BtoCRuntimeApi);

并将新的复合阶段定义为 stageAtoB.viaMat(stageBtoC)(combineIntoAtoC) 其中 combineIntoAtoC: (Future[AtoBRuntimeApi], Future[B2CRuntimeApi]) => Future[AtoCRuntimeApi].

显然 combineIntoAtoC 的实现需要一些 ExecutionContext 的实例来映射期货。

问题:在描述的情况下我应该使用什么执行上下文?

选项,我宁愿避免:

最需要的执行上下文是作为实体化器 (mat.executionContext) 的 属性 可用的执行上下文。但是我无法在该组合函数中访问它。

我通常在这种情况下使用actor系统上下文,默认情况下确实可以从materializer中引用。不过,作为通用解决方案,您可以将执行上下文传递给使用隐式参数构造流图的任何函数:

def makeGraph(...)(implicit ec: ExecutionContext): RunnableGraph = {
  def combineIntoAtoC(...) = ...  // uses ec implicitly
}

这使您可以决定将哪个上下文用完调用堆栈。在适当的级别,肯定会有某种访问 actor 系统的调度程序的权限。

我之所以更喜欢使用 actor 系统的调度程序而不是全局调度程序,是因为它减少了依赖性的表面 - 在这种情况下所有执行上下文都来自一个源,如果有需要。