jenkinsci / java-client-api

A Jenkins API client for Java
MIT License
896 stars 468 forks source link

when we use JenkinsHttpClient, we'd better need socket timeout for unstable net #488

Closed Wang-Yiran closed 2 years ago

Wang-Yiran commented 2 years ago

in class of JenkinsHttpClient, we need to set socket timeout and readtimeout to prevent the infinite socket waiting, which caused the thread block.

XWTiger commented 2 years ago

public JenkinsHttpClient(URI uri, HttpClientBuilder builder) { this(uri, initTimeOut(builder)); }

protected static CloseableHttpClient initTimeOut(HttpClientBuilder builder) {
    RequestConfig requestConfig = RequestConfig.custom()
            //从连接池中获取连接的超时时间
            .setConnectionRequestTimeout(6000)
            //与服务器连接超时时间:httpclient会创建一个异步线程用以创建socket连接,
            //此处设置该socket的连接超时时间
            .setConnectTimeout(6000)
            //socket读数据超时时间:从服务器获取响应数据的超时时间
            .setSocketTimeout(6000)
            .build();
    CloseableHttpClient httpClient = builder.setMaxConnTotal(50).setMaxConnPerRoute(1000).setDefaultRequestConfig(requestConfig).build();
    return httpClient;
}
Wang-Yiran commented 2 years ago

Thanks!