2017-05-18 124 views
0

我有一個請求正在與需要時間響應的後端數據庫交談。 http4s正在拋出請求超時。我想知道是否有一個屬性來增加請求超時?如何增加HTTP4的請求超時

謝謝 Saad。

回答

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 
)