# StreamTcpException for incoming requests

**URL:** <https://discuss.akka.io/t/streamtcpexception-for-incoming-requests/8956>\
**Category:** Akka HTTP\
**Created:** [October 26, 2021, 7:36am UTC](https://discuss.akka.io/t/streamtcpexception-for-incoming-requests/8956 "2021-10-26T07:36:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tg44](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/tg44/32/1099_2.png) [@tg44](https://discuss.akka.io/u/tg44)\
**Post date:** [October 26, 2021, 7:36am UTC](https://discuss.akka.io/t/streamtcpexception-for-incoming-requests/8956/1 "2021-10-26T07:36:57Z")

</div>

Hy!

I have a custom binding the code is exactly this;

```scala
    val settings = ServerSettings(actorSystem)
    val (bind, done) = Http()
      .newServerAt("0.0.0.0", 9000)
      .connectionSource()
      .mapAsyncUnordered(settings.maxConnections) { in =>
        var req: Option[HttpRequest] = None
        in.handleWith(
          Flow[HttpRequest]
            .mapAsync(1) { in =>
              req = Some(in)
              routeGen().flatMap(_(in))
            }
            .watchTermination()(Keep.right)
        ).recover {
          case ex =>
            req.fold {
              logger.info("Routing error!", ex)
              Done
            } { req =>
              logger.info(s"Routing error! on ${req.method} ${req.uri}", ex)
              Done
            }
        }
      }
      .toMat(Sink.ignore)(Keep.both)
      .run()

```

And I get;

```auto
2021-10-26 07:27:20 INFO main.AkkaWebserver$ - Routing error!
akka.stream.StreamTcpException: 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.
