akka.stream with TCP as input
In recent post You are evicted! I was planning to use akka.net streams solution to solve memory pressure problem. Today I have a solution that I will be testing.
Mainly I used an example from akka.net manual pages. As I was stuck on the flow (it is a bidirectional flow, where every byte received can have a response to the sender) I asked on the akka.net discord server for help as I was stuck in one of the stages, and my code was not compiling.
The TCP flow is very specific as it needs to start with ByteStream and finish with same, as what ever is on the end of the flow is sent directly to the other connected side.
The flow has stages listed below:
- Framing - that splits incoming stuff in chunks by delimiter (new line in our case, but can be a series of bytes as well)
- Then we are converting it to string
- Grouping (this is a key for the solution as sending one line over http is a killer)
- Final stage is the place where it will be send over http, but for illustrative purposes in gist there is only a loop writing to console received elements.
dotnet add package Akka.Streams --version 1.4.49
Let's run it for a quick test, network bind is to localhost port 8888 and if you have telnet enabled in your windows then just type: telnet localhost 8888 (linux geeks can use whatever they have)
It is hard to see on a static image, but our input was buffered and then printed, all 10 entries as per grouping stage parameter.
Important: if the client disconnects after let's say 3 lines, they will be processed so the data will be flushed.
Next test was done using a very simple code snippet that is pushing a lot of data over the wire, so the expectation is that the consumer will slow down the producer (using a backpressure mechanism).
Comments
Post a Comment