【问题标题】:How to increase request timeout for http4s如何增加http4s的请求超时
【发布时间】:2017-05-31 08:05:41
【问题描述】:

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

谢谢 萨德。

【问题讨论】:

    标签: http4s


    【解决方案1】:

    服务器超时

    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
    )
    

    【讨论】:

      猜你喜欢
      • 2013-01-23
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多