6

Using akka http to bind to a port and then route incoming connections is easy enough given the documentation.

One question not addressed is how to bind multiple ports for different routes. If I have multiple specifications:

val route1 : Flow[HttpRequest, HttpResponse,_] = ???
val interface1 : String = ???
val port1 : Int = ???

val route2 : Flow[HttpRequest, HttpResponse,_] = ???
val interface2 : String = ???
val port2 : Int = ???

Should these be bound with one HttpExt?

implicit val actorSystem : akka.actor.ActorSystem = ???

val httpExt = akka.http.scaladsl.Http()

httpExt.bindAndHandle(route1, interface1, port1)
httpExt.bindAndHandle(route2, interface2, port2)

Or, should a different HttpExt be used for each bind?

Http().bindAndHandle(route1, interface1, port1)
Http().bindAndHandle(route2, interface2, port2)

If either is valid, then are there any implications for using one method over the other?

Thank you in advance for your review and response.

Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125
  • This will answet your question: Http() eq Http() – Viktor Klang Sep 11 '16 at 10:18
  • @viktorklang That does add some clarity to the question. But, if an invocation of `Http ()` has any side effects, e.g. on the `ActorSystem`, then object equality wouldn't completely explain the behavior. – Ramón J Romero y Vigil Sep 11 '16 at 20:03
  • In my answer below I have a reference to the code that shows that second `Http()` call does not have any side effects. – Tim Sep 12 '16 at 03:42

1 Answers1

7

A little source code investigation shows the following.

Here we see what happens at Http() call. Http object extends ExtensionId[HttpExt] trait, which is responsible for registering additional functionality with actor system. This is that super call. As we can see it uses this reference, which in our case in an object reference (important). Here is what actually happen when we call Http(). The first thing this code does is checking whether the extension is already registered. Remember, that ext in our case is an object, so any subsequent calls after first one won't do anything.

This means that either of your approaches would work the same, but imo using one httpExt is less confusing, given how the underlying system really works.

Tim
  • 2,008
  • 16
  • 22