HTTP1.1 / 2.0 protocol and QUIC

HTTP request Construction

  1. Request line
  • Request method, such as get post put delete
  1. Header field

    • key value, such as Accept-Charset indicates that the client can accept the character set, to prevent pass over another character set, resulting in garbled.

      Content-Type refers to the text format, for example, post request, if the body is json should set this value to json

Sending the HTTP request

To send a link-oriented approach, by way of stream binary stream transmitted Oh the other side, to the tcp layer, put the binary stream into one packet to the server.

Each message sent to need each other to respond to ack, if no response has been sent, tcp each send a message needs to add the source address of the destination address to drink, put ip head inside, to the transport layer ip.

See ip layer destination address and whether they are the same local area network, if protocol request sends arp mac address corresponding to the destination address, source and destination mac mac mac head into the transmission, if not in the same local area network, send to the network, arp required transmission protocol, the gateway acquires mac, source mac mac head and sent into the gateway mac.

Gateway receives a packet in line with mac found, remove the target ip, find the next hop routing according to the routing protocol, get the next hop mac, the packet to the next hop router. The ultimate goal of reaching the local area network, this time finding the target address of the last hop in their exports of certain local area network, then sends arp across the LAN to obtain the objective mac address, the package sent out.

In line with its target discovery mac address, ip address matches, analytical tcp head, the serial number, if correct will return a ack, not discarded. tcp ahead with a port number, http server is listening on port number, so the target machine know the http server that you want to process this package, then the package will be sent to the http server.

HTTP return Construction

This is the status code you are familiar with such as 200,201,301,302,403,404,500

Header field key value, such as Retry-After, represent the client should try to tell how long, content-type returns html or json, then the same process when the latter process and send a request sent by the client. This is a normal http request to complete the process and return.

HTTP 2.0

HTTP 1.1 在应用层以纯文本的形式进行通信,每次通信都要带完整的http头,而且不考虑pipeline模式的话,每次过程在实时行和并发性都存在问题。

http2.0 对http的头进行压缩,将每次携带的大量key value在两端建立一个索引表,对相同的头只发送索引表中的索引。

http协议将一个tcp的连接中,切分为多个流,每个流都有自己的id,而且流可以是一个双向的虚拟通道,流也是有优先级别的。http中还有所有的传输消息分割为更小的信息和真,并对他们采用二进制格式编码。

通过这两种机制,http2.0的客户端可以讲多个请求分到不同的流中,将请求内容拆成帧,这些帧可以乱序传送,根据帧首部的流标识符重新组装,根据优先级决定处理哪个流的数据。

比如一个页面要发送三个独立的请求,分别获取css,js,jpg,如果使用http1.1就是串行的,如果使用2.0就可以在一个链接里,客户端和服务端可以同时发送多个请求或回应,而不是按照顺序一对一对应。

HTTP2.0解决了http1.1的队首阻塞问题,减少了tcp连接数对服务器性能的影响,同时加快了页面组件的传输速度。

QUCI协议

虽然2.0是可以解决1.1的高并发阻塞问题,但是2.0也是基于tcp洗衣的,当其中任何一个包遇到问题,都会阻塞住。

机制一:自定义连接机制

tcp连接是由四元组标识的,分别是源ip,源端口,目的ip,目的端口,任何一个元素发生变化时,就需要重新连接。再次进行三次握手,导致延时,在udp中,是以一个64位随机数作为id来标识,而且是无连接的,只要id不变就不需要重新建立连接。

机制二:自定义重传机制

quic有个序列号是递增的,任何一个序列号的包只发送一次,下次加一。例如,发送一个包,序号是100,发现没有返回,再次发送就是101,如果返回的ack是100,就是对第一个包的响应,返回ack101是第二个包的响应。quic还定义了一个offset概念,发送的数据在这个数据流离有个偏移量offset,可以通过offset查看数据发送到哪里,只要这个offset的包没有来就重发,如果来了就拼接。

机制三:无阻塞的多路复用

有了自定义连接和重传机制,就可以解决上面http2.0的多路复用问题。同一条QUIC连接上可以创建多个stream,发送多个http请求,但是quic是基于udp的,一个链接上多个stream之间没有依赖,这样,如果stream2丢了一个udp,后面跟着stream3的udp,虽然stream需要重传,但是stream3的包无需等待就可以发送。

机制四:自定义流量控制

tcp的流量控制是通过滑动窗口协议,quic通过window_update,来告诉对端它可以接受的字节数,但是quic的窗口是适应自己的多路复用机制的,不但在一个链接上控制窗口,还在一个连接中的每个stream控制窗口。

在tcp协议中,接收端的窗口的起点是下一个要接受并且ack的包,即便后来的包到了,放在缓存里,窗口也不能右移,只要前面的没到,后面的到了也不能ack,导致后面的到了,也有可能超时重传,浪费贷款。

quic的ack是基于offset的,每个offset包来了,进了缓存就可以应答,应答后不会重发,中间的空档会等待到来或者重发即可,而窗口的起始位置就是当前收到的最大offset,从这个offset到当前的stream所能容纳的最大缓存,就是真正窗口大小。

Guess you like

Origin www.cnblogs.com/jimmyhe/p/11260530.html