I'm trying to use akka-streams and akka-http in order to solve the following problem:
- we have 2 http clients (A and B) sending requests to 1 http server (C). Both side use akka-http to communicate.
- requests from A have higher priority compared to B, but both requests should be processes equally. So C should first process requests from A, requests from B are of second priority
- of course we would like to have back-pressure on each end enabled
I come up with the following code in order to get incoming connections merged to one output:
val g = RunnableGraph.fromGraph(FlowGraph.create() { implicit b: FlowGraph.Builder[Unit] =>
import FlowGraph.Implicits._
val merge = b.add(MergePreferred[IncomingConnection](1))
val inA: Source[IncomingConnection, Future[ServerBinding]] = Http().bind(interface = "localhost", port = 8200)
val inB: Source[IncomingConnection, Future[ServerBinding]] = Http().bind(interface = "localhost", port = 8201)
inA ~> merge.preferred
inB ~> merge.in(0)
merge.out ~> Sink.foreach(println)
ClosedShape
}).run()
So, I have a Source with IncomingConnection inctances from A and B.
Now I want to process them somehow, produce responses and send responses to corresponding connections.
Maybe there are better ways to archive all these things but I could not find any example solving such problem in docs or questions from other people.
Also I guess the problem is quite common.
Thanks in advance for your help.