How to increase request timeout for http4s
Asked Answered
S

1

7

I have a request that is talking to a backend db that takes time to respond. And the http4s is throwing request timeout. I wanted to know if there is a property to increase the request timeout?

Thanks Saad.

Severance answered 18/5, 2017 at 23:40 Comment(0)
A
7

Server Timeouts

BlazeBuilder can easily be adjusted. The default implementation is

import org.http4s._
import scala.concurrent.duration._

BlazeBuilder(
  socketAddress = InetSocketAddress.createUnresolved(LoopbackAddress, 8080),
  serviceExecutor = DefaultPool, // @org.http4s.util.threads - ExecutorService

  idleTimeout = 30.seconds
  isNio2 = false,

  connectorPoolSize = math.max(4, Runtime.getRuntime.availableProcessors() + 1),
  bufferSize = 64*1024,
  enableWebSockets = true,

  sslBits = None,
  isHttp2Enabled = false,

  maxRequestLineLen = 4*1024,
  maxHeadersLen = 40*1024,

  serviceMounts = Vector.empty
)

We can utilize the default and change that, as the class has a copy method implemented.

import org.http4s._
import scala.concurrent.duration._

BlazeBuilder.copy(idleTimeout = 5.minutes)

You can then proceed with your server however you would like, adding your services and then serving.

Client Timeouts

BlazeClient takes a config class called BlazeClientConfig

The default is

import org.http4s._
import org.http4s.client._

BlazeClientConfig(
  idleTimeout = 60.seconds,
  requestTimeout = Duration.Inf,
  userAgent = Some(
    `User-Agent`(AgentProduct("http4s-blaze", Some(BuildInfo.version)))
  ),

  sslContext = None,
  checkEndpointIdentification = true,

  maxResponseLineSize = 4*1024,
  maxHeaderLength = 40*1024,
  maxChunkSize = Integer.MAX_VALUE,
  lenientParser = false,

  bufferSize = 8*1024,
  customeExecutor = None,
  group = None
)

However we have a default config and as it exists as a case class you would probably be better modifying the default. Use PooledHttp1Client under most cases.

import scala.concurrent.duration._
import org.http4s.client._

val longTimeoutConfig =
  BlazeClientConfig
    .defaultConfig
    .copy(idleTimeout = 5.minutes)

val client = PooledHttp1Client(
  maxTotalConnections = 10,
  config = longTimeoutConfig
)
Anthropomorphize answered 31/5, 2017 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.