Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutionContext to use with mapAsync in Akka-Streams

I am just getting started with Akka Stream and I am trying to figure something out:

Currently, in my flows I am using mapAsync() to integrate with my rest services, as recommended here.

I have been wondering, what execution context should the mapAsync() be using? Should it be the dispatcher of my ActorSystem? The global? Are there any non-obvious consequences in either case?

I realize it's probably a silly question, but I've never dealt with Akka previously, and in any scala apps involving Futures, I've only ever used the global execution context.

like image 532
Emil D Avatar asked Mar 08 '16 00:03

Emil D


1 Answers

The mapAsync stage doesn't need an execution context, it only requires you to map the current stream element to a Future. The future's execution context depends on who creates it, the flow doesn't know anything about it.

More generally, a Future[A] is an abstraction that doesn't require you to know where it's running. It could even be a precomputed value that doesn't need an execution context:

def mappingFunction(x: Int) = Future.successful(x * 2)
Source(List(1, 2, 3)).mapAsync(1)(mappingFunction)

You only need to worry about ExecutionContexts when you create the Future, but in the case of mapAsync you're just returning one from a function. How to create the future is the function's responsibility. As far as the mapAsync stage is concerned, it just gets the future as the return value of the function, i.e. it doesn't create it.

Flows have a Materializer. Its current implementation is the ActorMaterializer, which materializes streams using an ActorSystem (and its dispatchers). You are not required to know the details of stream materialization though, streams work on a more abstract level and hypothetically you could have a different Materializer that doesn't work with an ActorSystem

like image 88
Giovanni Caporaletti Avatar answered Sep 21 '22 08:09

Giovanni Caporaletti