- 浏览: 3502933 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wanglf1207:
EJB的确是个不错的产品,只是因为用起来有点门槛,招来太多人吐 ...
weblogic-ejb-jar.xml的元素解析 -
qwfys200:
总结的不错。
Spring Web Flow 2.0 入门 -
u011577913:
u011577913 写道也能给我发一份翻译文档? 邮件437 ...
Hazelcast 参考文档-4 -
u011577913:
也能给我发一份翻译文档?
Hazelcast 参考文档-4 -
songzj001:
DbUnit入门实战
HTTP Persistent Connections
Persistent Connections
What is HTTP Persistent Connections?
HTTP persistent connections, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using the same TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new one for every single request/response pair. Using persistent connections is very important for improving HTTP performance.
There are several advantages of using persistent connections, including:
- Network friendly. Less network traffic due to fewer setting up and tearing down of TCP connections.
- Reduced latency on subsequent request. Due to avoidance of initial TCP handshake
- Long lasting connections allowing TCP sufficient time to determine the congestion state of the network, thus to react appropriately.
The advantages are even more obvious with HTTPS or HTTP over SSL/TLS. There, persistent connections may reduce the number of costly SSL/TLS handshake to establish security associations, in addition to the initial TCP connection set up.
In HTTP/1.1, persistent connections are the default behavior of any connection. That is, unless otherwise indicated, the client SHOULD assume that the server will maintain a persistent connection, even after error responses from the server. However, the protocol provides means for a client and a server to signal the closing of a TCP connection.
What makes a connection reusable?
Since TCP by its nature is a stream based protocol, in order to reuse an existing connection, the HTTP protocol has to have a way to indicate the end of the previous response and the beginning of the next one. Thus, it is required that all messages on the connection MUST have a self-defined message length (i.e., one not defined by closure of the connection). Self demarcation is achieved by either setting the Content-Length header, or in the case of chunked transfer encoded entity body, each chunk starts with a size, and the response body ends with a special last chunk.
What happens if there are proxy servers in between?
Since persistent connections applies to only one transport link, it is important that proxy servers correctly signal persistent/or-non-persistent connections separately with its clients and the origin servers (or to other proxy servers). From a HTTP client or server's perspective, as far as persistence connection is concerned, the presence or absence of proxy servers is transparent.
What does the current JDK do for Keep-Alive?
The JDK supports both HTTP/1.1 and HTTP/1.0 persistent connections.
When the application finishes reading the response body or when the
application calls close() on the InputStream
returned by
URLConnection.getInputStream(), the JDK's HTTP protocol handler
will try to clean up the connection and if successful, put the connection into a
connection cache for reuse by future HTTP requests.
The support for HTTP keep-Alive is done transparently. However, it can be
controlled by system properties http.keepAlive
, and
http.maxConnections
, as well as by HTTP/1.1 specified request and
response headers.
The system properties that control the behavior of Keep-Alive are:
http.keepAlive=<boolean>
default: true
Indicates if keep alive (persistent) connections should be supported.
http.maxConnections=<int>
default: 5
Indicates the maximum number of connections per destination to be kept alive at any given time
HTTP header that influences connection persistence is:
Connection: close
If the "Connection" header is specified with the value "close" in either the request or the response header fields, it indicates that the connection should not be considered 'persistent' after the current request/response is complete.
The current implementation doesn't buffer the response body. Which means that the application has to finish reading the response body or call close() to abandon the rest of the response body, in order for that connection to be reused. Furthermore, current implementation will not try block-reading when cleaning up the connection, meaning if the whole response body is not available, the connection will not be reused.
What's new in Tiger?
When the application encounters a HTTP 400 or 500 response, it may ignore the
IOException
and then may issue another HTTP request. In this case,
the underlying TCP connection won't be Kept-Alive because the response body is
still there to be consumed, so the socket connection is not cleared, therefore
not available for reuse. What the application needs to do is call
HttpURLConnection.getErrorStream()
after catching the
IOException
, read the response body, then close the stream.
However, some existing applications are not doing this. As a result, they do not
benefit from persistent connections. To address this problem, we have introduced
a workaround.
The workaround involves buffering the response body if the response is >=400, up to a certain amount and within a time limit, thus freeing up the underlying socket connection for reuse. The rationale behind this is that when the server responds with a >=400 error (client error or server error. One example is "404: File Not Found" error), the server usually sends a small response body to explain whom to contact and what to do to recover.
Several new Sun implementation specific properties are introduced to help clean up the connections after error response from the server.
The major one is:
sun.net.http.errorstream.enableBuffering=<boolean>
default:
false
With the above system property set to true (default is false), when the
response code is >=400, the HTTP handler will try to buffer the response
body. Thus freeing up the underlying socket connection for reuse. Thus, even if
the application doesn't call getErrorStream()
, read the response
body, and then call close(), the underlying socket connection may still be
kept-alive and reused.
The following two system properties provide further control to the error stream buffering behavior:
sun.net.http.errorstream.timeout=<int> in millisecond
default:
300 millisecond
sun.net.http.errorstream.bufferSize=<int> in bytes
default:
4096 bytes
What can you do to help with Keep-Alive?
Do not abandon a connection by ignoring the response body. Doing so may results in idle TCP connections. That needs to be garbage collected when they are no longer referenced.
If getInputStream()
successfully returns, read the entire
response body.
When calling getInputStream()
from
HttpURLConnection
, if an IOException
occurs, catch the
exception and call getErrorStream()
to get the response body (if
there is any).
Reading the response body cleans up the connection even if you are not interested in the response content itself. But if the response body is long and you are not interested in the rest of it after seeing the beginning, you can close the InputStream. But you need to be aware that more data could be on its way. Thus the connection may not be cleared for reuse.
Here's a code example that complies to the above recommendation:
try { URL a = new URL(args[0]); URLConnection urlc = a.openConnection(); is = conn.getInputStream(); int ret = 0; while ((ret = is.read(buf)) > 0) { processBuf(buf); } // close the inputstream is.close(); } catch (IOException e) { try { respCode = ((HttpURLConnection)conn).getResponseCode(); es = ((HttpURLConnection)conn).getErrorStream(); int ret = 0; // read the response body while ((ret = es.read(buf)) > 0) { processBuf(buf); } // close the errorstream es.close(); } catch(IOException ex) { // deal with the exception } }
If you know ahead of time that you won't be interested in the response body, you should issue a HEAD request instead of a GET request. For example when you are only interested in the meta info of the web resource or when testing for its validity, accessibility and recent modification. Here's a code snippet:
URL a = new URL(args[0]); URLConnection urlc = a.openConnection(); HttpURLConnection httpc = (HttpURLConnection)urlc; // only interested in the length of the resource httpc.setRequestMethod("HEAD"); int len = httpc.getContentLength();
Future directions in the JDK?
- More aggressive clean-up. An example is measured blocking read when application calls close before finish reading the response body
- pipelining requests to further increase the network bandwidth utilization and reduce latency
发表评论
-
字符串分割--java中String.split()用法
2013-03-06 14:25 74151在java.lang包中有String.sp ... -
用 HttpServletResponseWrapper 实现 Etag 过滤器
2012-07-09 16:58 3759原文出处:http://blog.chenlb.com/200 ... -
Fitnesse使用
2012-05-05 13:27 23494Fitnesse 的使用 一,介绍 Fitnesse是一种 ... -
Customizing the new FitNesse parser
2012-05-05 13:13 2134FitNesse began its life using ... -
java application中内嵌ActiveX控件
2011-11-14 15:57 5524我这里用的是SWT/JFace开发application,SW ... -
Google Java Developer Tools Downloads
2011-08-09 00:04 2347WindowBuilder Pro原来叫WindowB ... -
Jalita
2011-08-06 00:49 1565Jalita (Java light terminal ada ... -
【转】用Java写字符终端界面
2011-07-29 13:13 2122终端界面GUI开源项目charva。 这个框架让你可以用开发 ... -
[转]mybatis下的分页,支持所有的数据库
2011-07-21 13:21 14841大 家都知道,mybatis的自带分页方法只是逻 ... -
Java framework for text- & console-based forms?
2011-07-21 01:06 1711charva jcurses JNA , ... -
JNA(Java Native Access)学习入门
2011-07-21 01:04 22625Java Native Access 项目 在 ... -
JAVA上加密算法的实现用例
2011-06-25 12:38 4884来源:www.ibm.com ... -
如何将GlassFish作为Windows服务运行
2011-05-18 23:21 2375本文档来自GlassFish官方网站,详细介绍了将 G ... -
JAVA UDP打洞必备知识点---NAT
2011-05-05 12:56 8699一、引言 RFCl631 ... -
Keystore概念,Keytool工具使用
2011-04-28 16:20 2906近来由于项目需要做Single Sign On, 研究了一 ... -
利用Eclipse Profile Plugin监控分析Tomcat性能
2011-04-18 16:14 3702目前新版本的Eclipse在启动应用服务器的时候有一个新的选 ... -
m2eclipse: Eclipse is running in a JRE, but a JDK is required
2011-02-04 23:43 2542Eclipse 安装了Maven插件,启动Eclipse ... -
利用JNative实现Java调用动态库
2010-10-18 00:43 2100由于项目要求,需要用J ... -
RHEL5支持大内存
2010-10-08 16:19 3005安装 RHEL 5 ,硬件为 4G 内存,安装完成 ... -
Windows Server 2003 和 Windows 2000 提供大内存支持
2010-10-08 16:19 1854本文介绍物理地址扩展 ...
相关推荐
persistent connections and hubs; multiplatform real-time applications; advanced topics Learn how SignalR uses Websockets when supported by the browser and the server and falls back to other ...
该版本修正了: 1. 2009-10-21 Small bug fix for CD-images on Windows Vista and Windows 7. Support for 64-bit systems. 2.... Allow to mount images without ... Added support for persistent connections.
Starting from getting persistent connections with the server, you will learn the basics of connecting a client to the server and how the messaging works. This will be followed by setting up a hub on ...
Next, meet the concepts of hubs and persistent connections and how to use them to build the components of an ASP.NET SignalR application. Find out how to extend, test, debug, configure, scale, and ...
Next, meet the concepts of hubs and persistent connections and how to use them to build the components of an ASP.NET SignalR application. Find out how to extend, test, debug, configure, scale, and ...
elasticsearch-py uses persistent connections inside of individual connection pools (one per each configured or sniffednode). Outoftheboxyoucanchoosebetweentwo ...
4. **Persistent Connections**:对于不使用 Hub 模式的简单推送,SignalR 也提供了 Persistent Connections 接口,但在这个聊天室示例中,我们主要关注 Hub 模式。 5. **Transport机制**:SignalR 自动检测并使用...
... 1.3 Client Methods 1.4 Server Response Codes 1.5 Headers 1.6 URL Encoding 1.7 Client and Server Identification 1.8 Referring Documents ... 1.13 Persistent Connections 1.14 Client Caching
4. **Persistent Connections**:除了 Hub 模式,SignalR 还提供了 Persistent Connections 模式,用于更底层的实时通信。这种模式下,服务器可以直接与客户端进行通信,但没有 Hub 那样的对象模型和方法调用的便利...
This is an Open Source [MPL] SOAP client ...HTTP 1.1 support including persistent connections, SSL, proxies, authentication, proxy authentication, redirects, cookies and compression.
5. **持久连接(Persistent Connections)**:除了Hub之外,SignalR还提供了更底层的Persistent Connections API,适用于那些不需要服务器端状态或更细粒度控制的场景。 6. **集成与部署**:SignalR可以轻松地与ASP...
4. **Persistent Connections**: 在更底层,SignalR 提供了 Persistent Connections API,用于直接处理实时通信的细节,但通常推荐使用更高级别的 Hub API。 **三、ASP.NET 环境中的 SignalR 集成** 要在 ASP.NET ...
4. **Persistent Connections**:SignalR 使用 Persistent Connections 实现了长轮询、WebSockets、Server-Sent Events (SSE) 和 Forever Frame 等多种实时通信协议,根据客户端和服务器的支持自动选择最佳的传输...
HTTP 1.1 在HTTP/1.0的基础上引入了可持续链接(persistent connections)的概念,旨在减少频繁的TCP连接建立和关闭,以降低网络延迟和提高性能。 在HTTP/1.0中,每次请求/响应交互都会创建一个新的TCP连接,这对于...
The library supports persistent connections, which means that once a connection is established with an Elasticsearch cluster, it remains open for subsequent requests. This feature improves performance...
5. **持久连接**:SignalR如何处理网络断开和重新连接,以及如何使用Persistent Connections进行低级控制。 6. **实时通信示例**:通过实际的聊天室、实时计分板或协作编辑器等示例,展示SignalR的实时功能。 7. *...
4. **Persistent Connections**:除了Hub模式,SignalR还提供了更底层的Persistent Connection API,适用于不需要服务器端的方法调用的简单推送场景。 在实现ASP.NET SignalR的服务器推送功能时,你需要以下步骤: ...
4. **Persistent Connections**: 在不使用Hub的情况下,SignalR也支持更底层的Persistent Connection API,它提供了一种直接发送和接收消息的方式,适用于不需要复杂逻辑的情况。 **SignalR的工作原理** SignalR...
Persistent and non-persistent connections refer to how multiple data segments are handled after the connection is established. While they can impact overall transmission efficiency, they do not alter...
为了改进这些问题,HTTP/1.1在1997年发布,引入了持久连接(Persistent Connections)特性,允许在一个TCP连接上处理多个HTTP请求,大大提高了效率。此外,HTTP/1.1还引入了分块编码、内容协商、缓存控制等一系列...