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:

  1. Framing - that splits incoming stuff in chunks by delimiter (new line in our case, but can be a series of bytes as well)
  2. Then we are converting it to string
  3. Grouping (this is a key for the solution as sending one line over http is a killer)
  4. 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.
To test it on your local machine - one need to create a .net console project and add one nuget:
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).


In above snippet, the message payload need to be large or we need to add more than 5 seconds sleep time in the processor, to see the test results easily.

Below a screenshot when we can see that the process was blocked for around 5 seconds, so the slow consumer was able to do it's job.









Comments

Popular posts from this blog

when a new guy joins a team...

FizzBuzz - my first interview task on whiteboard

are YOU a garbage collector?