You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "wenqi.huang (JIRA)" <ji...@apache.org> on 2017/12/01 07:57:00 UTC

[jira] [Created] (HTTPCLIENT-1887) tcp status remains in ·CLOSE_WAIT· for a very long time

wenqi.huang created HTTPCLIENT-1887:
---------------------------------------

             Summary: tcp status remains in ·CLOSE_WAIT· for a very long time
                 Key: HTTPCLIENT-1887
                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1887
             Project: HttpComponents HttpClient
          Issue Type: Bug
          Components: HttpClient (classic)
    Affects Versions: 4.5.3
            Reporter: wenqi.huang


run the following code,  after the request completed, then wait for some seconds, you can use `netstat -an | grep {the server ip}` to see the tcp status, the tcp status will transform from `ESTABLISHED` to `CLOSE_WAIT`. and it remains in `CLOSE_WAIT` for a very long time, this only happen when the server want to close the tcp connection, but the client does not send `FIN` to the server. 

{code:java}
package cn.com.duibaboot.ext.autoconfigure.httpclient;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;

public class HttpClientTest1 {

    public static void main(String[] args) throws InterruptedException {
        final CloseableHttpClient httpClient = HttpClientBuilder
                .create()
                .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy(){
                    @Override
                    public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                        long time = super.getKeepAliveDuration(response, context);
                        if(time == -1){
                            time = 30000;
                        }
                        return time;
                    }
                })
                .build();

        HttpUriRequest req = new HttpGet("http://www.duiba.com.cn");
        try {
            CloseableHttpResponse r = httpClient.execute(req);
            IOUtils.toByteArray(r.getEntity().getContent());
            r.getEntity().getContent().close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Thread.sleep(Integer.MAX_VALUE);
    }
}

{code}




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org