概念
HTTP短连接(非持久连接)是指,客户端和服务端进行一次HTTP请求/响应之后,就关闭连接。所以,下一次的HTTP请求/响应操作就需要重新建立连接。
HTTP长连接(持久连接)是指,客户端和服务端建立一次连接之后,可以在这条连接上进行多次请求/响应操作。持久连接可以设置过期时间,也可以不设置。
我为什么没有说HTTP/1.0 默认短连接,HTTP/1.1起,默认长连接呢?因为我第一次看这个说法的时候,以为自己懂了,其实并没有懂。长短连接操作上有什么区别,有的地方出现的持久连接又是怎么回事?
使用设置
这里的设置,我们都以HTTP1.1协议为例子。
设置HTTP短连接
在首部字段中设置Connection:close,则在一次请求/响应之后,就会关闭连接。
设置HTTP长连接,有过期时间
在首部字段中设置Connection:keep-alive 和Keep-Alive: timeout=60,表明连接建立之后,空闲时间超过60秒之后,就会失效。如果在空闲第58秒时,再次使用此连接,则连接仍然有效,使用完之后,重新计数,空闲60秒之后过期。
设置HTTP长连接,无过期时间
在首部字段中只设置Connection:keep-alive,表明连接永久有效。
实现原理
了解怎么设置之后,就开始用起来。然而,问题来了。在请求头中设置Connection:keep-alive,为什么连接空闲一段时间之后,还是断开了呢?这是因为connection字段只有服务端设置才有效。
HTTP操作是请求/响应成对出现的,即先有客户端发出请求,后有服务端处理请求。所以,一次HTTP操作的终点操作在服务端上,关闭也是由服务端发起的。
接下来我们做做测试,以及show code。下面的测试都是使用Spring RestTemplate,封装apache http client进行的。为方便讲解代码,先说明长连接的情况,最后再对其他形式做测试总结。
客户端连接失效时间大于服务端失效时间
如下,为请求日志。客户端设置Connection: Keep-Alive和Keep-Alive: timeout=60, 服务端设置Connection: Keep-Alive和Keep-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 connection final 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,则返回false if (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 @Override public 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的设置来决定的。