Short and long connection link Http protocol introduced

Jane reproduced in the book:

Author: Bear Master
link: https: //www.jianshu.com/p/3fc3646fad80

1. The previous misunderstanding

I heard a long time before saying too long connection, but also know HTTP1.0 protocol does not support long connection, From HTTP1.1 agreement, default connection is a long connection. But in the end felt for a long connection has been muddle, a kind of feeling can not find the key.

Today, through some research, and finally understand the mystery of it. Previously, we have seen long connection-related content, but has been foggy. This has been able to figure out in such a short period of time, and settling himself technologies are inseparable. So, here take this opportunity to emphasize once again, do not try to study something you studied for a long time are whole do not understand, perhaps less than your level, or maybe you've never been in contact with the actual application scenarios In this case you go to research, only half done, nothing vain about it.

Back to the topic, since that is misunderstood, so misunderstood in the end is what?

That's always thought, HTTP connection is divided into short and long connection connected, and we are now commonly used HTTP1.1, therefore we are a long connection.

In fact, this sentence is only half right, we now HTTP protocol, mostly 1.1, so we usually use are basically long connection. But first phrase is wrong, HTTP protocol did not say that the length of the connection, but also because of this misunderstanding, leading to a long connection has been unclear, not always its essentials, we will discuss some concrete below.

Many articles on the web are fraught did not understand that concept. Here we must emphasize, HTTP protocol is based on request / response mode, so as long as the server to the response, this HTTP connection is over, or, more precisely, is this the HTTP request is over, there is no longer connected With that said. So naturally there is no short connecting this to say.

The reason why the network is divided into long HTTP connections and short connections, in fact, essentially said TCP connection. TCP connection is a two-way channel, it can be maintained for a period of time does not close, so TCP connections have real short and long connection connecting the said.

In fact, knew what was coming, you will find this is well understood. HTTP protocol application layer protocol in the final analysis, while the TCP transport layer protocol is the real and only responsible for the transmission of this layer only needs to establish a connection.

One example is the image, take your online shopping for, HTTP protocol means that express orders, fill in your mailing list is like when an HTTP request is made, and other goods to the place, the courier will according to your request to send the goods to the consignee accordingly. The TCP protocol is the middle of the big cargo truck, train or plane it may be, but no matter what, it is responsible for transport, and therefore must have a road, whether on the ground or the sky. Well, this road is called TCP connection, which is a bi-directional data channel.

Therefore, LZ and even now feel, "HTTP connection" is the word should not appear, it's just an application layer protocol, there is no so-called connection that said, like FTP protocol is the application layer, but you have heard over FTP connections? (Well, it seems that heard -_-, but now you know, in fact, the so-called FTP connection, strictly speaking, is still a TCP connection)

Indeed, he said HTTP request and HTTP response would be more accurate, the HTTP request and HTTP response, a TCP connection this channel is transmitted back and forth.

不管怎么说,一定要务必记住,长连接是指的TCP连接,而不是HTTP连接。

A question

Before LZ has been somewhat confusing one thing, even if the first is how to become a long HTTP connection, you are not just set Connection to a keep-alive even?

If yes, then say HTTP1.1 default connection is long, but we usually observe the HTTP header developing Web applications, Connection indeed is keep-alive, that is to say most of them are long connection we use, but long connections are not generally used to interact more frequent applications? Like us ordinary Web applications, such as garden this blog, or my personal blog this long connection with what?

If the use is useful in the end what it is that we are not the client and server applications that interact frequently (after all, you certainly have to open the page a long time to open another bar), if useless, then in the end we should not be to keep the Connection -alive to get rid of this header value, and thus into a short connection?

This question, in fact, LZ understand the long connection means after the TCP connection, basically understand. And this question, it is also LZ in the "previous misunderstanding" that period mentioned, that misunderstanding has led to LZ do not understand the question.

Why then solves the above misunderstanding, said before these questions LZ understand everything?

Because of the long connection means that the connection can be reused, after all, it has maintained a connection not just to re-use thing. But if long connection means of HTTP, then it means that HTTP connection can be reused, if this sounds awkward situation. The reason feel uncomfortable, actually a kind of intuition LZ, nothing theoretical basis. The source of this awkward is that, until there is no feeling of mastery, so always feel something is missing. But this doubt, did not affect the LZ work, and therefore no get to the bottom too.

But now, to understand the long connection actually refers to TCP connections, LZ himself instantly wanted to understand those issues above.

The first question is, is not just set to Connection keep-alive even longer connected?

Of course it is, but to set up both the server and client.

The second problem is that we usually use is not long connection?

This is also no doubt, of course, yes. (Basically HTTP1.1 agreement now using, you look you will find, are basically Connection keep-alive. And on the HTTP protocol document also mentioned, HTTP1.1 default connection is long, which is the default Connection the value is the keep-alive)

The third question, before most LZ also do not understand the problem, and that is that we ordinary Web applications (such as blog garden, my personal blog this) with long connections so what are the benefits? Or need to switch off long connection using a short connection?

This problem LZ now finally understand, the answer is some good news.

What are the benefits?

First of all, have already said, a long connection to reuse, before the LZ will understand. Since that long connection is a TCP connection, that reuse is a TCP connection. It is much easier to explain, say, in the long connections, multiple HTTP request can reuse the same TCP connection, which saves consume a lot of TCP connection establishment and disconnection.

For example, you request a web page blog garden, there is certainly this page also includes CSS, JS and so a series of resources, if you are short connection (that is, each time to re-establish a TCP connection), then every time you open a web pages, basic to establish several or even dozens of TCP connections, which do not have the amount of resources wasted LZ to say it.

But if it is a long connection, then so many HTTP requests (these requests, including requests Web content, CSS files, JS files, pictures, etc.), are actually using a TCP connection, it is clear that you can save a lot of consumption.

As a result interpretation, it is clear, do not know how to explain the feeling you read, anyway, they want to understand LZ in the future, suddenly a kind of feeling.

In addition, the last on a long connection even more the way, that is, long connection is not permanently connected. If the period of time (the specific length of time, can be set in the header of which, the so-called timeout), the HTTP request, then the connection is not issued, then the length of the connection will be broken.

This is actually very easy to understand, otherwise, TCP connection will be more and more, until the number of TCP connection to the server to explode to the upper limit. Now think about it, for the server, the server where the connection is actually very long taste database connection pool, we are all connected reuse in order to save thing, right?

Long and short polling polling

LZ has put in front of basically almost the length of said connection, then talk about the length of the polling, today precisely in order to study the length of the polling, LZ was under way to study the length of the connection thing.

Short polling believe we are not difficult to understand, like you do now electricity supplier in a listings page, this detail screen has a field that inventory (to believe that everyone is familiar with, or not open Taobao Jingdong can be found this page). And this requires real-time inventory changes, and maintaining servers in the same physical inventory.

This time, how would you do?

The easiest one way that you write with JS death cycle, stop to ask for inventory server is how much, then refresh this page to them, this is in fact the so-called short polling.

This approach has obvious disadvantages, that is, you're a waste of resources and client server. The client better point, now high PC machine configuration, you keep the request is still not the user's computer to death, but the server is very egg pain. If there are 1,000 people remain in a product detail page, that means there will be 1000 client requests the server to stop acquiring inventory, which is obviously unreasonable.

then what should we do?

Long polling this time there was, in fact, the biggest difference between long and short polling polling is polling to serve the short end of the query time, no matter the amount of inventory has not changed, the server immediately return the results. And polling is not long, the long polling, the server if it is detected no change in the amount of stock, it will suspend the current request period of time (this time is also called the timeout period, typically tens of seconds). At this time, the server to detect inventory levels have not changed, change is detected to return immediately, otherwise wait until the timeout.

As for the client, whether it is long or short poll to poll, the client's actions are the same, it is to keep to the request, except that the service side, under circumstances short polling the server for every request no matter no change will immediately return the results, and the next long polling, if there is a change would immediately return a result, and there is no change, then the client will not immediately return a result, until the timeout.

As a result, the number of requests the client will be a significant reduction (which means savings in network traffic, after all, every request made, traffic will occupy download the client and server upload traffic), but also to solve the server has been exhausted to accept the request of the dilemma.

But the long polling is a bad thing, because the same request pending would lead to a waste of resources, assuming that 1,000 people still remain in some commodity details page, it is likely the server side threads hanging from 1000, kept detection of inventory, this is still a problem.

Thus, it can be seen here, whether short or long polling polling, are much less suitable for the case where the number of clients, since the server TCP connections per can carry is capped, this wheel Inquiry is easy to connect several top full. The reason For example, just because I am sure you are online shopping, so this example a little more popular.

Even polling can not solve this problem acquiring inventory, but as long as we understand the difference between the length of the polling, it was enough. In fact, it was observed that LZ their daily shopping, the inventory should not change, this example is purely personal LZ obscenity, -_-.

Polling and the length difference between the length of the connecting

Here briefly about the difference between them, LZ just say the most fundamental difference here.

The first difference is the way the decision, whether a TCP connection is a persistent connection, by setting the HTTP Connection Header determined, but both sides need to set effective. A wrap mode and whether the long polling, the server is based on the approach to the decision, there is no relationship with the client.

The second difference is the way to achieve, to a predetermined length is connected and implemented by agreement. The length of the polling is done manually suspend request server programmatically.

 

Guess you like

Origin www.cnblogs.com/Teachertao/p/11141276.html