HTTP管线化(HTTP pipelining) 默认情况下http协议中每个传输层连接只能承载一个http请求和响应,然后结束。

HTTP是一个简单的协议。客户进程建立一条同服务器进程的 T C P连接,然后发出请求并读取服务器进程的响应。服务器进程关闭连接表示本次响应结束。服务器进程返回的文件通常含有指向其他服务器上文件的指针(超文本链接)。用户显然可以很轻松地沿着这些链接从一个服务器到下一个服务器。

HTTP管线化

HTTP管线化是将多个HTTP要求(request)整批提交的技术,而在传送过程中不需先等待服务端的回应。管线化机制须通过永久连接(persistent connection)完成,仅HTTP/1.1支持此技术(HTTP/1.0不支持),并且只有GET和HEAD要求可以进行管线化,而POST则有所限制。此外,初次创建连接时也不应启动管线机制,因为对方(服务器)不一定支持HTTP/1.1版本的协议。

浏览器将HTTP要求大批提交可大幅缩短页面的加载时间,特别是在传输延迟(lag/latency)较高的情况下(如卫星连接)。此技术之关键在于多个HTTP的要求消息可以同时塞入一个TCP分组中,所以只提交一个分组即可同时发出多个要求,借此可减少网络上多余的分组并降低线路负载。

[摘自W3C]

在永久连接或者HTTP pipelining出现之前,每个连接的获取都需要创建一个独立的TCP连接。

Prior to persistent connections, a separate TCP connection was established to fetch each URL, increasing the load on HTTP servers and causing congestion(拥塞) on the Internet.

[摘自维基百科]

HTTP pipelining可以吧多个http请求输出到一个socket连接(倘若是基于socket的话,更确切的说应该是输出到一个传输层连接,在tcp ip中即为TCP连接)中去,然后等到对应的响应。

HTTP pipelining is a technique in which multiple HTTP requests are written out to a single socket without waiting for the corresponding responses. Pipelining is only supported in HTTP/1.1, not in 1.0.

The pipelining of requests results in a dramatic improvement in page loading times, especially over high latency connections such as satellite Internet connections.

Since it is usually possible to fit several HTTP requests in the same TCP packet, HTTP pipelining allows fewer TCP packets to be sent over the network, reducing network load.

Non-idempotent(非幂等) methods like POST should not be pipelined. Sequences of GET and HEAD requests can be always pipelined. A sequence of other idempotent requests like GET, HEAD, PUT and DELETE can be pipelined or not depending on whether requests in the sequence depend on the effect of others.[1]

HTTP pipelining requires both the client and the server to support it. HTTP/1.1 conforming servers are required to support pipelining. This does not mean that servers are required to pipeline responses, but that they are required not to fail if a client chooses to pipeline requests.

Implementation in web servers

Implementing pipelining in web servers is a relatively simple matter of making sure that network buffers are not discarded between requests. For that reason, most modern web servers handle pipelining without any problem.
Exceptions include IIS 4.

Implementation in web browsers

HTTP pipelining is disabled in most browsers.
Opera has pipelining enabled by default. It uses heuristics to control the level of pipelining employed depending on the connected server.
Internet Explorer 8 does not pipeline requests, due to concerns regarding buggy proxies and head-of-line blocking.
Mozilla browsers (such as Mozilla Firefox, SeaMonkey and Camino), support pipelining however it is disabled by default.It uses some heuristics(测试试探), especially to turn pipelining off for IIS servers. does the same thing as Firefox.
Konqueror 2.0 supports pipelining, but it’s disabled by default.
Google Chrome does not support pipelining.

Implementation in web proxies
Most HTTP proxies do not pipeline outgoing requests.
Some versions of the Squid web proxy will pipeline up to two outgoing requests. This functionality has been disabled by default and needs to be manually enabled for “bandwidth management and access logging reasons.”Squid supports multiple requests from clients.
The Polipo proxy pipelines outgoing requests.

浏览:6 views 阅读全文

 这篇文章主要是从原理, 手册和源码分析在PHP中查询MySQL返回大量结果时, 内存占用的问题, 同时对使用MySQL C API也有涉及.

  昨天, 有同事在PHP讨论群里提到, 他做的一个项目由于MySQL查询返回的结果太多(达10万条), 从而导致PHP内存不够用. 所以, 他问, 在执行下面的代码遍历返回的MySQL结果之前, 数据是否已经在内存中了? -

  while ($row = mysql_fetch_assoc($result)) {

  // …

  }

  当然, 这种问题有许多优化的方法. 不过, 就这个问题来讲, 我首先想到, MySQL是经典的C/S(Client/Server, 客户端/服务器)模型, 在遍历结果集之前, 底层的实现可能已经把所有的数据通过网络(假设使用TCP/IP)读到了Client的缓冲区, 也有另一种可能, 就是数据还在Server端的发送缓冲区里, 并没有传给Client.

  在查看PHP和MySQL的源码之前, 我注意到PHP手册里有两个功能相近的函数:

  mysql_query()

  mysql_unbuffered_query()

  两个函数的字面意思和说明证实了我的想法, 前一个函数执行时, 会把所有的结果集从Server端读到Client端的缓冲区中, 而后一个则没有, 这就是”unbuffered(未缓冲)”的意思.

  那就是说, 如果用mysql_unbuffered_query()执行了一条返回大量结果集的SQL语句, 在遍历结果之前, PHP的内存是没有被结果集占用的. 而用mysql_query()来执行同样的语句的话, 函数返回时, PHP的内存占用便会急剧增加, 立即耗光内存.

  如果阅读PHP的相关代码, 可以看到这两个函数的实现上的异同:

  /* {{{ proto resource mysql_query(string query [, int link_identifier])

  Sends an SQL query to MySQL */

  PHP_FUNCTION(mysql_query)

  {

  php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_STORE_RESULT);

  }

  /* }}} */

  /* {{{ proto resource mysql_unbuffered_query(string query [, int link_identifier])

  Sends an SQL query to MySQL, without fetching and buffering the result rows */

  PHP_FUNCTION(mysql_unbuffered_query)

  {

  php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_USE_RESULT);

  }

  /* }}} */

  两个函数都调用了php_mysql_do_query(), 只差了第2个参数的不同, MYSQL_STORE_RESULT和MYSQL_USE_RESULT. 再看php_mysql_do_query()的实现:

  if(use_store == MYSQL_USE_RESULT) {

  mysql_result=mysql_use_result(&mysql->conn);

  } else {

  mysql_result=mysql_store_result(&mysql->conn);

  }

  mysql_use_result()和mysql_store_result()是MySQL的C API函数, 这两个C API函数的区别就是后者把结果集从MySQL Server端全部读取到了Client端, 前者只是读取了结果集的元信息.

  回到PHP, 使用mysql_unbuffered_query(), 可以避免内存的立即占用. 如果在遍历的过程不对结果进行”PHP缓存”(如放到某数组中), 则整个执行过程虽然操作了十万条或者百万条或者更多的数据, 但PHP占用的内存始终是非常小的.

TechTarget中国原创内容,原文链接:http://www.searchdatabase.com.cn/showcontent_45803.htm

浏览:5 views 阅读全文
Gravatar

阻止EditText在有焦点时弹出输入法

伯温 | 发表于:2012-02-21 | 移动终端

0

//当EidtText获取焦点,此方法不能阻止输入法弹出
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
//imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
//EditText有焦点阻止输入法弹出
editText.setOnTouchListener(new OnTouchListener() {

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		int inType = editText.getInputType(); // backup the input type
		editText.setInputType(InputType.TYPE_NULL); // disable soft input
		editText.onTouchEvent(event); // call native handler
		editText.setInputType(inType); // restore input type
		editText.setSelection(editText.getText().length());
		return true;
		}
	});
浏览:5 views 阅读全文