HTTP长链接、短链接使用及测试

概念

HTTP短链接(非持久链接)是指,客户端和服务端进行一次HTTP请求/响应以后,就关闭链接。因此,下一次的HTTP请求/响应操做就须要从新创建链接。
HTTP长链接(持久链接)是指,客户端和服务端创建一次链接以后,能够在这条链接上进行屡次请求/响应操做。持久链接能够设置过时时间,也能够不设置。javascript

我为何没有说HTTP/1.0 默认短链接,HTTP/1.1起,默认长链接呢?由于我第一次看这个说法的时候,觉得本身懂了,其实并无懂。长短链接操做上有什么区别,有的地方出现的持久链接又是怎么回事?html

使用设置

这里的设置,咱们都以HTTP1.1协议为例子。java

设置HTTP短链接

在首部字段中设置Connection:close,则在一次请求/响应以后,就会关闭链接。apache

设置HTTP长链接,有过时时间

在首部字段中设置Connection:keep-alive 和Keep-Alive: timeout=60,代表链接创建以后,空闲时间超过60秒以后,就会失效。若是在空闲第58秒时,再次使用此链接,则链接仍然有效,使用完以后,从新计数,空闲60秒以后过时。json

设置HTTP长链接,无过时时间

在首部字段中只设置Connection:keep-alive,代表链接永久有效。c#

实现原理

了解怎么设置以后,就开始用起来。然而,问题来了。在请求头中设置Connection:keep-alive,为何链接空闲一段时间以后,仍是断开了呢?这是由于connection字段只有服务端设置才有效。api

HTTP操做是请求/响应成对出现的,即先有客户端发出请求,后有服务端处理请求。因此,一次HTTP操做的终点操做在服务端上,关闭也是由服务端发起的。
接下来咱们作作测试,以及show code。下面的测试都是使用Spring RestTemplate,封装apache http client进行的。为方便讲解代码,先说明长链接的状况,最后再对其余形式作测试总结。浏览器

客户端链接失效时间大于服务端失效时间

以下,为请求日志。客户端设置Connection: Keep-AliveKeep-Alive: timeout=60, 服务端设置Connection: Keep-AliveKeep-Alive: timeout=5服务器

## 客户端设置有效期为60s[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "POST /adx-api/api/creative/upload HTTP/1.1[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Accept: application/json, application/*+json, text/html, application/json, text/javascript[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Content-Type: application/json;charset=UTF-8[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Accept-Language: zh-CN[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: keep-alive[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Keep-Alive: timeout=60[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Content-Length: 396[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Host: bizdomain[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "request data"##服务端设置有效期为5s[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Date: Wed, 26 Apr 2017 06:07:58 GMT[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Server: Apache-Coyote/1.1[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Content-Type: text/html;charset=utf-8[\r][\n]"[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Keep-Alive: timeout=5, max=100[\r][\n]"[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Connection: Keep-Alive[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Transfer-Encoding: chunked[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "63[\r][\n]"
[2017-04-26 14:08:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "response data"

客户端设置的有效期大于服务端的,那么实际链接的有效期呢?三分钟以后再次请求,从链接池中lease链接的时候,提示Connection expired @ Wed Apr 26 14:08:05,即在上一次请求以后的5s失效,说明是服务端的设置生效了。网络

[2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200]
[2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.CPool:?) - Connection [id:2][route:{}->http://bizdomain:80][state:null] expired @ Wed Apr 26 14:08:05 GMT+08:00 2017

源码分析

经过源代码了解一下链接失效时间的设置过程。

//org.apache.http.impl.execchain.MainClientExec#execute......//从链接池中lease connectionfinal HttpClientConnectionmanagedConn = connRequest.get(timeout > 0 ? timeout : 0, TimeUnit.MILLISECONDS);

......//将conenction封装在ConnectionHolder中final ConnectionHolder connHolder = new ConnectionHolder(this.log, this.connManager, managedConn);

......// The connection is in or can be brought to a re-usable state.//若是返回值消息头中connection设置为close,则返回falseif (reuseStrategy.keepAlive(response, context)) {    // Set the idle duration of this connection
    //取出response消息头中,keep-alive的timeout值
    final long duration = keepAliveStrategy.getKeepAliveDuration(response, context);    if (this.log.isDebugEnabled()) {        final String s;        if (duration > 0) {
            s = "for " + duration + " " + TimeUnit.MILLISECONDS;
        } else {
            s = "indefinitely";
        }        this.log.debug("Connection can be kept alive " + s);
    }    //设置失效时间
    connHolder.setValidFor(duration, TimeUnit.MILLISECONDS);
    connHolder.markReusable();
} else {
    connHolder.markNonReusable();
}

待读取响应以后,释放链接,即:connHolder.releaseConnection()。调用org.apache.http.impl.conn.PoolingHttpClientConnectionManager#releaseConnection方法。

    @Override
    public void releaseConnection(final HttpClientConnection managedConn, 
            final Object state,final long keepalive, final TimeUnit tunit) {
        Args.notNull(managedConn, "Managed connection");        synchronized (managedConn) {            final CPoolEntry entry = CPoolProxy.detach(managedConn);            if (entry == null) {                return;
            }            final ManagedHttpClientConnection conn = entry.getConnection();            try {                if (conn.isOpen()) {                    final TimeUnit effectiveUnit = tunit != null ? tunit : TimeUnit.MILLISECONDS;
                    entry.setState(state);                    //设置失效时间
                    entry.updateExpiry(keepalive, effectiveUnit);
                }
            } finally {
            。。。。。。
                }
            }
        }
    }

而后再下一次HTTP操做,从链接池中获取链接时

//org.apache.http.impl.conn.PoolingHttpClientConnectionManager#requestConnection调用org.apache.http.pool.AbstractConnPool#lease,//调用getPoolEntryBlocking,调用org.apache.http.impl.conn.CPoolEntry#isExpired@Overridepublic boolean isExpired(final long now) {    final boolean expired = super.isExpired(now);    if (expired && this.log.isDebugEnabled()) {    //日志中看到的内容
        this.log.debug("Connection " + this + " expired @ " + new Date(getExpiry()));
    }    return expired;
}

综上,链接的实际有效时间,是根据response的设置来决定的。

其余状况测试

  • 客户端设置Connection: Close

##connection:close请求,kept alive的链接为0[2017-04-26 13:57:00 DEBUG]  (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200]
[2017-04-26 13:57:00 DEBUG]  (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection leased: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 200]
[2017-04-26 13:57:00 DEBUG]  (org.apache.http.impl.execchain.MainClientExec:?) - Opening connection {}->http://bizdomain:80
[2017-04-26 13:57:00 DEBUG]  (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:?) - Connecting to bizdomain/127.0.0.195:80## 创建新链接[2017-04-26 13:57:00 DEBUG]  (org.apache.http.impl.conn.DefaultHttpClientConnectionOperator:?) - Connection established 127.0.0.191:49239<->127.0.0.195:80## 客户端设置短链接[2017-04-26 13:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: Close[\r][\n]"## 服务端返回的也是短链接[2017-04-26 13:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Connection: close[\r][\n]"

##请求完以后,关闭链接
[2017-04-26 13:57:00 DEBUG]  (org.apache.http.impl.conn.DefaultManagedHttpClientConnection:?) - http-outgoing-0: Close connection[2017-04-26 13:57:00 DEBUG]  (org.apache.http.impl.execchain.MainClientExec:?) - Connection discarded[2017-04-26 13:57:00 DEBUG]  (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection released: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200]

如上,当服务端返回Connection: Close时,客户端接收完响应,便会关闭链接。

  • 客户端设置60s超时,服务端设置5s超时

##Keep-Alive: timeout=60 第一次请求,与connection:close无差异[2017-04-26 10:57:00 DEBUG]  (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200]
[2017-04-26 10:57:00 DEBUG]  (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection leased: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 200]
[2017-04-26 10:57:00 DEBUG]  (org.apache.http.impl.execchain.MainClientExec:?) - Opening connection {}->http://bizdomain:80## 客户端设置超时时间60s[2017-04-26 10:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: keep-alive[\r][\n]"
[2017-04-26 10:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Keep-Alive: timeout=60[\r][\n]"## 服务端设置超时时间5s[2017-04-26 10:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Keep-Alive: timeout=5, max=100[\r][\n]"[2017-04-26 10:57:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Connection: Keep-Alive[\r][\n]"
## 服务端设置生效,链接能够保持5s[2017-04-26 10:57:00 DEBUG]  (org.apache.http.impl.execchain.MainClientExec:?) - Connection can be kept alive for 5000 MILLISECONDS[2017-04-26 10:57:00 DEBUG]  (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection [id: 0][route: {}->http://bizdomain:80] can be kept alive for 5.0 seconds
[2017-04-26 10:57:00 DEBUG]  (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection released: [id: 0][route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200]##Keep-Alive: timeout=60 非第一次请求[2017-04-26 14:11:00 DEBUG]  (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200]## 链接在上一次请求结束后5s失效[2017-04-26 14:11:00 DEBUG]  (org.apache.http.impl.conn.CPool:?) - Connection [id:2][route:{}->http://bizdomain:80][state:null] expired @ Wed Apr 26 14:10:05 GMT+08:00 2017
  • 客户端设置失效时间,服务端设置不失效

## 客户端设置30s超时[2017-04-26 17:45:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Connection: keep-alive[\r][\n]"
[2017-04-26 17:45:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 >> "Keep-Alive: timeout=30[\r][\n]"## 服务端设置永久链接[2017-04-26 17:45:00 DEBUG]  (org.apache.http.wire:?) - http-outgoing-0 << "Connection: keep-alive[\r][\n]"
## 链接将一直保持
[2017-04-26 17:45:00 DEBUG]  (org.apache.http.impl.execchain.MainClientExec:?) - Connection can be kept alive indefinitely

综上,http链接保持时间是由服务端的消息头connection字段和keep-alive字段定的。

在上面前两种状况,请求的是同一个服务端,那么为何一个返回的是短链接,一个返回的是长链接呢?这里转一下 这篇文章的解释:

不论request仍是response的header中包含了值为close的connection,都代表当前正在使用的tcp连接在请求处理完毕后会被断掉。之后client再进行新的请求时就必须建立新的tcp连接了。 HTTP Connection的 close设置容许客户端或服务器中任何一方关闭底层的链接,双方都会要求在处理请求后关闭它们的TCP链接。

补充

  • TCP长短链接
    在网上搜资料的时候,看到不少“HTTP协议的长链接和短链接,实质上是TCP协议的长链接和短链接”。 HTTP和TCP是不一样两层的东西,它们怎么会是同样的呢?HTTP是请求/响应模式的,就是说咱们发一个请求必定要有一个回应。最直观的就是,浏览器上发请求,得不到响应就会一直转圈圈。 而TCP并非必定要有响应。你们之前使用socket模拟一个IM聊天,A跟B打完招呼,彻底能够不用等待B的回应,就本身关掉链接的。

  • TCP keep-alive
    另外还有HTTP协议的keep-alive和TCP的keep-alive含义是有差异的。HTTP的keep-alive是为了维持链接,以便复用链接。经过使用keep-alive机制,能够减小tcp链接创建次数,也意味着能够减小TIME_WAIT状态链接,以此提升性能和提升httpd服务器的吞吐率(更少的tcp链接意味着更少的系统内核调用,socket的accept()和close()调用)。可是,长时间的tcp链接容易致使系统资源无效占用。配置不当的keep-alive,有时比重复利用链接带来的损失还更大。

而tcp keep-alive是TCP的一种检测TCP链接情况的机制,涉及到三个参数tcp_keepalive_time, tcp_keepalive_intvl, tcp_keepalive_probes。当网络两端创建了TCP链接以后,闲置(双方没有任何数据流往来)了tcp_keepalive_time后,服务器内核就会尝试向客户端发送侦测包,来判断TCP链接情况(有可能客户端崩溃、强制关闭了应用、主机不可达等等)。若是没有收到对方的回答(ack包),则会在 tcp_keepalive_intvl后再次尝试发送侦测包,直到收到对方的ack。若是一直没有收到对方的ack,一共会尝试 tcp_keepalive_probes次。若是尝试tcp_keepalive_probes,依然没有收到对方的ack包,则会丢弃该TCP链接。TCP链接默认闲置时间是2小时,通常设置为30分钟足够了。

相关文章
相关标签/搜索