Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize Akka Http Client execution context

Tags:

akka-http

When calling singleRequest, how can one customize the execution context that is used by the connection pool? I took a brief look at the code, and a call to singleRequest results in a message being sent to the PoolMasterActor, which in turn sends a message to the pool interface actor.

  1. Is each connection blocking or non-blocking?
  2. Which context is used for the connection pool? (I want to make sure that my HTTP requests don't block all the threads)
like image 276
EugeneMi Avatar asked Feb 06 '17 00:02

EugeneMi


1 Answers

If you check out the singleRequest signature, it requires an implicit Materializer (and therefore an ActorSystem and its dispatchers) to run the underlying HTTP infrastructure - which is based on Akka Streams. More knowledge on how materializers spawn threads under-the-hood can be found in the docs, and this blogpost.

Going back to your questions:

  1. The whole Akka-HTTP infrastructure is inherently non-blocking (as it's based on Akka Streams - which adheres to the Reactive Streams spec and is based on Akka Actors).

  2. The threading used by the singleRequest call inherits from the ActorSystem dispatcher used down the line. Unless you do anything specific, you will end up using your system's default dispatcher. This is reasonable choice in many cases when you are writing an Akka HTTP client.

In case you really need your materializer to use a custom dispatcher you can achieve this by customizing your ActorMaterializerSettings, e.g.

implicit val materializer = ActorMaterializer(
  ActorMaterializerSettings(actorSystem).withDispatcher("my-custom-dispatcher")
)
like image 84
Stefano Bonetti Avatar answered Jan 02 '23 13:01

Stefano Bonetti