我需要使用Linux/C++/libcurl定期和随机地测试通过单个DNS名称可用的几个服务器的响应,例如
$ host example.com
n1.example.com 1.2.3.4
n2.example.com 1.2.3.5
n3.example.com 1.2.3.6
列表会发生变化。当我尝试TTL时,libcurl总是使用相同的IP作为https://example.com
的跨度,并且我不能切换到下一个主机。有easycurl setopt,但将其设置为0没有任何帮助-即使我完全重新创建了CURLOPT_DNS_CACHE_TIMEOUT
对象,我仍然得到相同的IP。因此,这没有帮助:curl - How to set up TTL for dns cache & How to clear the curl cache
当然,我可以手动解析DNS名称并迭代,但是有什么选择吗?随机轮询是可以的。我看到curl使用了c-ares。有没有办法清理那里的缓存,它会有帮助吗?
发布于 2021-07-15 02:57:22
如果我自己不下定决心,我就不能用curl做我需要的事情,但有一些发现可以分享给其他人:
首先,作为一个编写良好的TCP客户端,curl将从上到下尝试DNS列表中的主机,直到成功建立连接。从那时起,它将使用该主机,即使它返回一些更高级别错误(例如SSL错误或HTTP 500)。这对所有重大案件都有好处。
较新版本的Curl命令行有--retry
和--retry-all-errors
-但不幸的是,libcurl
中没有这样的东西。该功能目前正在增强,截至2021-07-14还没有一个版本会枚举所有DNS主机,直到有一个主机返回HTTP 200。相反,已发布的curl版本(我尝试了7.76和7.77)将始终在同一主机上进行重试。但是夜间构建(2021-07-14)确实枚举了所有DNS主机。下面是它对两次重试和三次不存在的主机的行为(请注意,如果任何主机返回HTTP 5xx,则会发生重试):
$ ./src/curl http://nohost.sureno --trace - --retry 2 --retry-all-errors
== Info: Trying 192.168.1.112:80...
== Info: connect to 192.168.1.112 port 80 failed: No route to host
== Info: Trying 192.168.1.113:80...
== Info: connect to 192.168.1.113 port 80 failed: No route to host
== Info: Trying 192.168.1.114:80...
== Info: connect to 192.168.1.114 port 80 failed: No route to host
== Info: Failed to connect to nohost.sureno port 80 after 9210 ms: No route to host
== Info: Closing connection 0
curl: (7) Failed to connect to nohost.sureno port 80 after 9210 ms: No route to host
Warning: Problem (retrying all errors). Will retry in 1 seconds. 2 retries
Warning: left.
== Info: Hostname nohost.sureno was found in DNS cache
== Info: Trying 192.168.1.112:80...
== Info: connect to 192.168.1.112 port 80 failed: No route to host
== Info: Trying 192.168.1.113:80...
== Info: connect to 192.168.1.113 port 80 failed: No route to host
== Info: Trying 192.168.1.114:80...
== Info: connect to 192.168.1.114 port 80 failed: No route to host
== Info: Failed to connect to nohost.sureno port 80 after 9206 ms: No route to host
== Info: Closing connection 1
curl: (7) Failed to connect to nohost.sureno port 80 after 9206 ms: No route to host
Warning: Problem (retrying all errors). Will retry in 2 seconds. 1 retries
这种行为对libcurl的用户非常有帮助,但不幸的是,这些重试标志目前没有映射到curl_easy_setopt
。因此,如果您将--libcurl
赋予命令行,您将不会看到任何与重试相关的代码
https://stackoverflow.com/questions/68315302
复制相似问题