5

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

谢谢萨德。

4

1 回答 1

4

服务器超时

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
)
于 2017-05-31T00:05:41.283 回答