Search code examples
scalaakkaakka-http

How to set dispatcher / thread pool for akka http


I have a question regarding akka-http. I have developed a service which is spinning up different actors and besides of some other task some of the actors are also doing external http calls to different endpoints via the akka-http Http().singleRequest function. I want to isolate the Http calls via a separate dispatcher pool/configuration, but I can't find a way to define the dispatcher. For other actors I can define it via the .withDispatcher() method. Akka-http is running via akka streams as far as I know (via the actor materializer), but how can I define the dispatcher for the Http().singleRequest function?

Thanks in advance!


Solution

  • you can define your dispatcher in your conf file. for more information please read akka documentation from the given link. Dispatchers implement the ExecutionContext interface and can thus be used to run Future invocations etc.

    actually after defining the dispatcher, you can use that dispatcher for the actor's which are doing the calls for external API and give them the execution context via dispatcher.

    https://doc.akka.io/docs/akka/2.5/dispatchers.html

    it has structure like this:

    my-dispatcher {
      # Dispatcher is the name of the event-based dispatcher
      type = Dispatcher
      # What kind of ExecutionService to use
      executor = "fork-join-executor"
      # Configuration for the fork join pool
      fork-join-executor {
        # Min number of threads to cap factor-based parallelism number to
        parallelism-min = 2
        # Parallelism (threads) ... ceil(available processors * factor)
        parallelism-factor = 2.0
        # Max number of threads to cap factor-based parallelism number to
        parallelism-max = 10
      }
      # Throughput defines the maximum number of messages to be
      # processed per actor before the thread jumps to the next actor.
      # Set to 1 for as fair as possible.
      throughput = 100
    }