Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase request timeout for http4s

Tags:

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.

like image 465
saad Avatar asked May 18 '17 23:05

saad


1 Answers

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
)
like image 123
ChristopherDavenport Avatar answered Sep 21 '22 10:09

ChristopherDavenport