akka-http

Akka-http process HttpRequests from different connections in one flow

南笙酒味 提交于 2019-12-07 10:08:46
问题 Akka-http documentation says: Apart from regarding a socket bound on the server-side as a Source[IncomingConnection] and each connection as a Source[HttpRequest] with a Sink[HttpResponse] Assume we get the merged source containing incoming connections from many Source[IncomingConnection]. Then, assume, we get Source[HttpRequest] from Source[IncomingConnection] (see the code below). Then, no problem, we can provide a flow to convert HttpRequest to HttpResponse. And here is the problem - how

Obtaining the client IP in Akka-http

不羁岁月 提交于 2019-12-07 05:47:47
问题 I am trying to write an Akka HTTP microservice (akka version 2.4.11, Scala version 2.11.8, both latest versions at time of writing) which is aware of the client service's IP (i.e., remote address), and I cannot get this to work. I can create and run a service which says 'Hello!' using a route like this: val routeHello: Route = path("SayHello") { get { entity(as[String]) { body => complete { HttpResponse(entity = HttpEntity("Hello!")) } } } } I have constructed a similar route to the one above

akka-stream + akka-http lifecycle

我们两清 提交于 2019-12-06 20:47:44
问题 TLDR: is it better to materialize a stream per request (i.e. use short-lived streams) or to use a single stream materialization across requests, when I have an outgoing http request as a part of the stream? Details: I have a typical service that takes an HTTP request, scatters it to several 3rd party downstream services (not controlled by me) and aggregates the results before sending them back. I'm using akka-http for client implementation and spray for server (legacy, will move to akka-http

Waiting for a client websocket flow to connect before connecting source and sink

老子叫甜甜 提交于 2019-12-06 16:03:32
I'm using akka-streams to set up a client web socket. I'm trying to encapsulate the setup in a method with the following signature: def createConnectedWebSocket(url: String): Flow[Message, Message, _] It is clear how to create the web socket flow but it is not connected yet: val webSocketFlow: Flow[Message, Message, Future[WebSocketUpgradeResponse]] = Http().webSocketClientFlow(WebSocketRequest(url)) I first want to Await the upgrade response future and then return the socket flow. However, in order to get the future, I have to materialize the flow and for that I have to connect a Source and a

How to do akka-http request-level backpressure?

岁酱吖の 提交于 2019-12-06 12:09:48
In akka-http, you can: Set akka.http.server.max-connections, which will prevent more than that number of connections. Exceeding this limit means clients will get connection timeouts. Set akka.http.server.pipelining-limit, which prevents a given connection from having more than this number of requests outstanding at once. Exceeding this means clients will get socket timeouts. These are both forms of backpressure from the http server to the client, but both are very low level, and only indirectly related to your server's performance. What seems better would be to backpressure at the http level,

How to send a file as a response using akka http?

天涯浪子 提交于 2019-12-06 08:12:48
I am a little new to akka world, so my domain of knowledge is a bit small. I am creating an https server and handling it using akka streams and http, for a specific url, i need to send a file back to client. How can i achieve that using akka streams and avoiding the akka routes. def handleCall(request:HttpRequest):HttpResponse = { logger.info("Request is {}",request) val uri:String = request.getUri().path() if(uri == "/download"){ val f = new File("/1000.txt") logger.info("file download") return HttpEntity( //What should i put here if i want to return a text file. ) } If the file is likely to

Akka Streams / HTTP: Get original request from response

本秂侑毒 提交于 2019-12-06 06:04:15
I have an Akka Streams source that goes through a flow and posts an HTTP request: source.map(toRequest) .via(Http().outgoingConnection(host)) .map(toMessage) Suppose that the toRequest method maps a string to an HttpRequest , and the toMessage method maps the HttpResponse to a message class that is needed to process downstream. Suppose that the message class needs to contain some of the original information. Is it possible to get the original HttpRequest from the HttpResponse ? If not, is there any way to keep some of the original information? One approach is to use a Future -based variant of

How to run Akka Streams graph on a separate dispatcher with timeout?

只谈情不闲聊 提交于 2019-12-06 02:32:14
This question is based on a pet project that I did and this SO thread. Inside a Akka HTTP route definition, I start a long-running process, and naturally I want to do that without blocking the user. I'm able to achieve this with the code snippet below: blocking-io-dispatcher { type = Dispatcher executor = "thread-pool-executor" thread-pool-executor { fixed-pool-size = 16 } throughput = 1 } complete { Try(new URL(url)) match { case scala.util.Success(u) => { val src = Source.fromIterator(() => parseMovies(u).iterator) src .via(findMovieByTitleAndYear) .via(persistMovies) .toMat(Sink.fold(Future

Akka HTTP Connection Pool Hangs After Couple of Hours

不羁的心 提交于 2019-12-06 00:02:22
I have an HTTP Connection Pool that hangs after a couple of hours of running: private def createHttpPool(host: String): SourceQueue[(HttpRequest, Promise[HttpResponse])] = { val pool = Http().cachedHostConnectionPoolHttps[Promise[HttpResponse]](host) Source.queue[(HttpRequest, Promise[HttpResponse])](config.poolBuffer, OverflowStrategy.dropNew) .via(pool).toMat(Sink.foreach { case ((Success(res), p)) => p.success(res) case ((Failure(e), p)) => p.failure(e) })(Keep.left).run } I enqueue items with: private def enqueue(uri: Uri): Future[HttpResponse] = { val promise = Promise[HttpResponse] val

Query parameters for GET requests using Akka HTTP (formally known as Spray)

孤者浪人 提交于 2019-12-05 14:23:14
问题 One of the features of Akka HTTP (formally known as Spray) is its ability to automagically marshal and unmarshal data back and forth from json into case classes, etc. I've had success at getting this to work well. At the moment, I am trying to make an HTTP client that performs a GET request with query parameters. The code currently looks like this: val httpResponse: Future[HttpResponse] = Http().singleRequest(HttpRequest( uri = s"""http://${config.getString("http.serverHost")}:${config.getInt