0

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.

yannisf
  • 6,016
  • 9
  • 39
  • 61

2 Answers2

1

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))
      }
Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
0

I think, you are looking for Future.within

Dima
  • 39,570
  • 6
  • 44
  • 70
  • In order for Future.within to throw a TimeoutException, you have to Await for it. Since Finatra callbacks are called from the Event loop thread, it is never a good idea to Await. – yannisf Jan 22 '20 at 20:13
  • No, you don't have to `Await`. It will complete with the exception after the time you specify, and the server will return a 500 or whatever you map that exception to. – Dima Jan 22 '20 at 22:01