scalafinatra

Set Finatra HTTP request timeout


Is it possible to set HTTP request-response timeout in a Finatra server?

The http controller callback typically returns a Future, that once resolved the response is transmitted. I would like to define, within Finatra, how long the server should wait before returning a 500 or 400 response.


Solution

  • You can extend the HttpServer and define your own timeout

        trait CustomServer extends HttpServer with Tls {
    

    then you overwrite the configureHttpServer method and you define timeout, requests sites, and other attributes

      override def configureHttpServer(server: Http.Server): Http.Server = {
        server.withAdmissionControl.concurrencyLimit(maxConcurrentRequests = 2000, maxWaiters = 0)
          .withResponseClassifier(HttpResponseClassifier.ServerErrorsAsFailures)
          .withMaxRequestSize(StorageUnit.fromMegabytes(200))
          .withRequestTimeout(50.seconds)
      }
    

    Or, a more minimal example in your class that extends Http.Server:

          override protected def configureHttpServer(server: Http.Server): Http.Server = {
            super.configureHttpServer(server.withRequestTimeout(50.seconds))
          }