StreamTcpException for incoming requests


I have a custom binding the code is exactly this;

    val settings = ServerSettings(actorSystem)
    val (bind, done) = Http()
      .newServerAt("", 9000)
      .mapAsyncUnordered(settings.maxConnections) { in =>
        var req: Option[HttpRequest] = None
            .mapAsync(1) { in =>
              req = Some(in)
        ).recover {
          case ex =>
            req.fold {
    "Routing error!", ex)
            } { req =>
    "Routing error! on ${req.method} ${req.uri}", ex)

And I get;

2021-10-26 07:27:20 INFO  main.AkkaWebserver$ - Routing error! The connection actor has terminated. Stopping now.

This means that the tcp connection is terminating BEFORE I get the chance to “cache” the incoming http request. The maxConnection is 2048, we definitely need to improve our metrics, but it seems that we are way under the limit. Most of our requests end with a 200 or 404 in a 40-300ms range (according to nginx). I think it is some kind of browser cut connection thingy, but I’m generally interested if others saw something like this, or I messed up something, or if there are any tips’n’tricks to improve the above code.