如何增加http4s的请求超时

时间:2017-05-18 23:40:17

标签: http4s

我有一个与后端数据库通信的请求需要时间来响应。并且http4s正在抛出请求超时。我想知道是否有一个属性来增加请求超时?

由于 萨阿德。

1 个答案:

答案 0 :(得分:2)

服务器超时

BlazeBuilder可以轻松调整。默认实现是

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
)

我们可以利用默认值并对其进行更改,因为该类已实现了复制方法。

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

BlazeBuilder.copy(idleTimeout = 5.minutes)

然后,您可以根据需要继续使用服务器,添加服务然后提供服务。

客户超时

BlazeClient采用名为BlazeClientConfig

的配置类

默认为

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
)

但是我们有一个默认配置,因为它作为案例类存在,你可能会更好地修改默认值。在大多数情况下使用PooledHttp1Client

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

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

val client = PooledHttp1Client(
  maxTotalConnections = 10,
  config = longTimeoutConfig
)
相关问题