如何增加http4s的请求超时

saa*_*aad 5 http4s

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

谢谢萨德。

Chr*_*ort 7

服务器超时

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
)
Run Code Online (Sandbox Code Playgroud)

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

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

BlazeBuilder.copy(idleTimeout = 5.minutes)
Run Code Online (Sandbox Code Playgroud)

然后,您可以按照您的意愿继续使用您的服务器,添加您的服务,然后提供服务。

客户端超时

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
)
Run Code Online (Sandbox Code Playgroud)

但是,我们有一个默认配置,并且由于它作为案例类存在,因此您可能最好修改默认配置。大多数情况下使用PooledHttp1Client 。

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

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

val client = PooledHttp1Client(
  maxTotalConnections = 10,
  config = longTimeoutConfig
)
Run Code Online (Sandbox Code Playgroud)