如何增加HTTP4的请求超时
答
服务器超时
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
)