akka-stream

akka-http: complete request with flow

杀马特。学长 韩版系。学妹 提交于 2019-11-30 07:12:49
Assume I have set up an arbitrarily complex Flow[HttpRequest, HttpResponse, Unit] . I can already use said flow to handle incoming requests with Http().bindAndHandle(flow, "0.0.0.0", 8080) Now I would like to add logging, leveraging some existing directive, like logRequestResult("my-service"){...} Is there a way to combine this directive with my flow? I guess I am looking for another directive, something along the lines of def completeWithFlow(flow: Flow): Route Is this possible at all? N.B.: logRequestResult is an example, my question applies to any Directive one might find useful. Turns out

Akka streams: Reading multiple files

纵然是瞬间 提交于 2019-11-30 05:47:16
问题 I have a list of files. I want: To read from all of them as a single Source. Files should be read sequentially, in-order. (no round-robin) At no point should any file be required to be entirely in memory. An error reading from a file should collapse the stream. It felt like this should work: (Scala, akka-streams v2.4.7) val sources = Seq("file1", "file2").map(new File(_)).map(f => FileIO.fromPath(f.toPath) .via(Framing.delimiter(ByteString(System.lineSeparator), 10000, allowTruncation = true)

Close akka-http websocket connection from server

只谈情不闲聊 提交于 2019-11-30 03:38:46
问题 In my scenario, a client sends "goodbye" websocket message and I need to close previously established connection at the server side. From akka-http docs: Closing connections is possible by cancelling the incoming connection Flow from your server logic (e.g. by connecting its downstream to a Sink.cancelled and its upstream to a Source.empty). It is also possible to shut down the server's socket by cancelling the IncomingConnection source connections. But it's not clear to me how to do that

Websocket Proxy using Play 2.6 and akka streams

帅比萌擦擦* 提交于 2019-11-29 22:38:44
问题 I'm trying to create a simple Proxy for Websocket connections using Play and akka streams. The traffic flow is like this: (Client) request -> -> request (Server) Proxy (Client) response <- <- response (Server) I came up with the following code after following some examples: def socket = WebSocket.accept[String, String] { request => val uuid = UUID.randomUUID().toString // wsOut - actor that deals with incoming websocket frame from the Client // wsIn - publisher of the frame for the Server val

How to use an Akka Streams SourceQueue with PlayFramework

让人想犯罪 __ 提交于 2019-11-29 22:26:18
I would like to use a SourceQueue to push elements dynamically into an Akka Stream source. Play controller needs a Source to be able to stream a result using the chuncked method. As Play uses its own Akka Stream Sink under the hood, I can't materialize the source queue myself using a Sink because the source would be consumed before it's used by the chunked method (except if I use the following hack). I'm able to make it work if I pre-materialize the source queue using a reactive-streams publisher, but it's a kind of 'dirty hack' : def sourceQueueAction = Action{ val (queue, pub) = Source.queue

akka-http send continuous chunked http response (stream)

て烟熏妆下的殇ゞ 提交于 2019-11-29 15:13:46
问题 I have this crude test example with akka-http client and server. Server.scala: import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.scaladsl.Sink import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpMethods._ import akka.http.scaladsl.model._ import scala.concurrent.Future class Server extends Runnable { def run() = { implicit val system = ActorSystem("server") implicit val materializer = ActorMaterializer() val serverSource = Http().bind

Alternative flows based on condition for akka stream

馋奶兔 提交于 2019-11-29 14:55:09
问题 Have a stream with custom flows and at a certain stage I want to split the stream and have two alternative data handling which will merge again later. E.g. -> F3 -> F6 Src -> F1 -> F2 > Merge -> Sink -> F4 -> F5 F2 should have a condition saying if data contains format A then it should go to flow F3 , else go to F4 . As far as I can see, each flow can only have one port in each direction (or two if bidi) - so how can I support such a flow? 回答1: You can use Broadcast to split the stream, then

How to limit an Akka Stream to execute and send down one message only once per second?

大兔子大兔子 提交于 2019-11-29 10:26:44
I have an Akka Stream and I want the stream to send messages down stream approximately every second. I tried two ways to solve this problem, the first way was to make the producer at the start of the stream only send messages once every second when a Continue messages comes into this actor. // When receive a Continue message in a ActorPublisher // do work then... if (totalDemand > 0) { import scala.concurrent.duration._ context.system.scheduler.scheduleOnce(1 second, self, Continue) } This works for a short while then a flood of Continue messages appear in the ActorPublisher actor, I assume

akka-http: complete request with flow

拜拜、爱过 提交于 2019-11-29 09:52:42
问题 Assume I have set up an arbitrarily complex Flow[HttpRequest, HttpResponse, Unit] . I can already use said flow to handle incoming requests with Http().bindAndHandle(flow, "0.0.0.0", 8080) Now I would like to add logging, leveraging some existing directive, like logRequestResult("my-service"){...} Is there a way to combine this directive with my flow? I guess I am looking for another directive, something along the lines of def completeWithFlow(flow: Flow): Route Is this possible at all? N.B.:

How are reactive streams used in Slick for inserting data

吃可爱长大的小学妹 提交于 2019-11-29 04:01:17
In Slick's documentation examples for using Reactive Streams are presented just for reading data as a means of a DatabasePublisher. But what happens when you want to use your database as a Sink and backpreasure based on your insertion rate? I've looked for equivalent DatabaseSubscriber but it doesn't exist. So the question is, if I have a Source, say: val source = Source(0 to 100) how can I crete a Sink with Slick that writes those values into a table with schema: create table NumberTable (value INT) Serial Inserts The easiest way would be to do inserts within a Sink.foreach . Assuming you've