简体   繁体   English

libcurl:连接保持活动如何工作?

[英]libcurl: how does connection Keep-Alive work?

How do I keep connection alive with libcurl?如何与 libcurl 保持连接?

The usage that I'd like is the following.我想要的用法如下。 I want to connect to a server that supports Keep-Alive but which closes the connection after 90 seconds of inactivity.我想连接到支持Keep-Alive但在 90 秒不活动后关闭连接的服务器。 I want to do POST once in a while when some events (outside of this connection) occur, and I want to keep the connection alive in order to reduce latency.当某些事件(在此连接之外)发生时,我想偶尔执行POST ,并且我想让连接保持活动状态以减少延迟。 It might happen that no event occurs for more than 90 seconds, so I want a way to tell the server I'm not idle.可能会发生超过 90 秒没有事件发生的情况,所以我想要一种方法来告诉服务器我没有空闲。

The examples page is excellent, but I don't see one involving Keep-Alive : https://curl.haxx.se/libcurl/c/example.html示例页面非常好,但我没有看到一个涉及Keep-Alivehttps : //curl.haxx.se/libcurl/c/example.html

I did find CURLOPT_TCP_KEEPINTVL : https://curl.haxx.se/libcurl/c/CURLOPT_TCP_KEEPINTVL.html , but it's not clear to me how it's supposed to work.我确实找到了CURLOPT_TCP_KEEPINTVLhttps://curl.haxx.se/libcurl/c/CURLOPT_TCP_KEEPINTVL.html ,但我不清楚它应该如何工作。 In that example, we see the code:在那个例子中,我们看到了代码:

CURL *curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");

  /* enable TCP keep-alive for this transfer */
  curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

  /* set keep-alive idle time to 120 seconds */
  curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L);

  /* interval time between keep-alive probes: 60 seconds */
  curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L);

  curl_easy_perform(curl);
}

The description says the following:说明如下:

Pass a long.长传。 Sets the interval, in seconds, that the operating system will wait between sending keepalive probes.设置操作系统在发送 keepalive 探测之间等待的时间间隔(以秒为单位)。 Not all operating systems support this option.并非所有操作系统都支持此选项。 (Added in 7.25.0) (在 7.25.0 中添加)

Two questions:两个问题:

A) Operationally, how do I use this? A) 在操作上,我如何使用它? From the description, it looks like as long as I keep curl in scope, the connection will remain open.从描述来看,只要我保持curl的范围,连接就会保持打开状态。 Is this correct?这样对吗? And then should I just keep doing然后我应该继续做吗

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
res = curl_easy_perform(curl);

whenever I want to POST more data and it will be sent in the same connection, or is there more to it?每当我想POST更多的数据,它会在同一个连接上发送,或者是有更多的东西?

B) What are these keepalive probes and where can I read more about them? B) 这些 keepalive 探针是什么,我在哪里可以阅读更多关于它们的信息? Is this something that the OS does behind my back?这是操作系统在我背后做的事情吗? Should I think of this as: from the point of view of my application, the connection is magically alive and I shouldn't worry about it?我应该把这看作是:从我的应用程序的角度来看,连接神奇地活着,我不应该担心吗?


Yes, I meant HTTP Keep-Alive , sorry for the ambiguity.是的,我的意思是 HTTP Keep-Alive ,抱歉含糊不清。

Since yesterday, I found that Daniel Stenberg himself has answered a similar question here: https://stackoverflow.com/a/14148171/12874900从昨天开始,我发现 Daniel Stenberg 自己在这里回答了一个类似的问题: https : //stackoverflow.com/a/14148171/12874900

This answers part A) of my question.这回答了我的问题的 A) 部分。 But it doesn't answer question B).但它不回答问题 B)。 I would also need to know: if the connection does die for whatever reason, how am I meant to detect it?我还需要知道:如果连接确实因任何原因死亡,我打算如何检测它? Or does libcurl take care of the reconnect?还是 libcurl 负责重新连接?

Someone feel free to edit this into my original post.有人可以随意将其编辑到我的原始帖子中。

HTTP persistent connections HTTP 持久连接

As said many times: libcurl does persistent HTTP connections by default.正如多次所说:默认情况下,libcurl 会执行持久的 HTTP 连接。 You just have to reuse the curl handles for that to work and you have to actively ask for not doing them persistent if you don't want them.你只需要重用 curl 句柄才能工作,如果你不想要它们,你必须主动要求不要让它们持久化。 It doesn't matter what HTTP method you use.您使用什么 HTTP 方法并不重要。 GET, POST, HEAD etc all maintain the connection - unless something out of the ordinary happens. GET、POST、HEAD 等都保持连接 - 除非发生异常情况。

Modern libcurl versions will however only reuse connections up CURLOPT_MAXAGE_CONN seconds (118 by default) - since the chance of successful reuse diminishes really quickly over time.然而,现代 libcurl 版本只会重用CURLOPT_MAXAGE_CONN秒(默认为 118 秒)的连接 - 因为成功重用的机会随着时间的推移会很快减少。

Sometimes you see clients use the Keep-Alive: header but that's legacy from the HTTP/1.0 days and is virtually pointless in this day and age.有时您会看到客户端使用Keep-Alive:标头,但这是 HTTP/1.0 时代的遗留问题,在当今时代几乎毫无意义。

If the connection closes after 90 seconds as claimed in this question, that is because the server closes idle connections then.如果连接在此问题中声称的 90 秒后关闭,那是因为服务器随后关闭了空闲连接。 Servers do that.服务器就是这样做的。 You can't prevent them in any other way than to use the connection again before the server kills it.除了在服务器杀死它之前再次使用连接之外,您无法以任何其他方式阻止它们。

TCP Keepalive TCP保活

The TCP keepalive options are at an entirely different level. TCP keepalive 选项处于完全不同的级别。 That's basically TCP sending a packet over the connection after N seconds of inactivity, in order to make sure the connection is kept alive (and to detect broken connections).这基本上是 TCP 在 N 秒不活动后通过连接发送数据包,以确保连接保持活动状态(并检测断开的连接)。

libcurl does not enable TCP keepalive by default, see CURLOPT_TCP_KEEPALIVE . libcurl 默认不启用 TCP keepalive,请参阅CURLOPT_TCP_KEEPALIVE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM