3
votes

What is the correct way to fire a load-balancer using akka http? Currently I’m using the follow piece of code:

val poolClientFlow = Http().cachedHostConnectionPool[String](host, port)

source
  .map(url => HttpRequest(uri = url) -> url)
  .via(poolClientFlow)
  .to(Sink.actorRef(myActor, IsDone))
  .run()

Since I open the connection before, the load balancer understand I want to send to the same machine, so the roundrobin does not work properly. Should I move the poolClientFlow to the .via, open and closing it every time?

1

1 Answers

2
votes

I wrote one a few weeks back as an experiment:

https://github.com/agemooij/stream-experiments/blob/master/src/main/scala/scalapenos/experiments/streams/LoadBalancingHttpClient.scala

It uses the graph DSL to balance the request over a number of separate connection pools.