[英]Good production values for CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT towards same server
I hope my question is not too arbitrary: On my production server, I have a page sending an API call to another app on the same server with cURL. 我希望我的问题不要太随心所欲:在生产服务器上,我有一个页面,使用cURL将API调用发送到同一服务器上的另一个应用程序。
I would like to set appropriate values on CURLOPT_CONNECTTIMEOUT
then CURLOPT_TIMEOUT
to leave enough time to make sure the request completes in case there are lots of requests, but at the same time I don't want the page to hang in case the API is down or takes too long to respond, which would then result in major lag for all users as a huge line-up would be generated. 我想先在
CURLOPT_CONNECTTIMEOUT
上设置适当的值,然后在CURLOPT_TIMEOUT
上留出足够的时间以确保在有大量请求的情况下完成请求,但是同时我不希望页面在API关闭或关闭的情况下挂起。需要太长时间才能做出响应,这将导致所有用户的重大滞后,因为将会产生庞大的阵容。
The API makes a really quick DB query that returns a small json structure, nothing complicated that takes much time. 该API可以进行非常快速的数据库查询,并返回一个小的json结构,没有什么复杂的事情需要花费很多时间。
Given these specifications, any people could comment on this? 有了这些规范,有人可以对此发表评论吗? I currently have:
我目前有:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
UPDATE : 更新 :
There are many factors that you need to consider when defining those values. 定义这些值时,需要考虑许多因素。 However, 2 seconds is considered acceptable for an HTTP request (first byte).
但是,对于HTTP请求(第一个字节),可以认为2秒是可以接受的。 If you manage the server where the HTTP request originates then you also should consider the server resources eg, memory, CPU, IO, web server configuration.
如果管理HTTP请求起源的服务器,则还应考虑服务器资源,例如内存,CPU,IO,Web服务器配置。 But generally speaking you should consider running traceroute to your end point to better understanding latency-- this will help you define a more accurate timeout values for CURL.
但是一般来说,您应该考虑在端点上运行traceroute以更好地了解延迟-这将帮助您为CURL定义更准确的超时值。 And of course, finally you should consider the same resource constraints mentioned earlier for the server for the client if you are initiating many requests from there.
当然,最后,如果您要从服务器发起许多请求,则应该考虑前面提到的服务器的相同资源限制。
Hope this helps! 希望这可以帮助! See blog at: https://www.inmotionhosting.com/support/website/how-to/read-traceroute
请参阅以下博客: https : //www.inmotionhosting.com/support/website/how-to/read-traceroute
You need to have information about the behaviour of that API in order to make an educated decision. 您需要了解有关该API行为的信息,以便做出有根据的决策。 It also depends on how that API is used in your code and what behaviour you desire in case of failure.
它还取决于在您的代码中如何使用该API以及在发生故障时所需的行为。
Consider: 考虑:
What's the target response time for your user facing site? 您面向用户的网站的目标响应时间是多少?
Network timeouts are typically between 30 and 120 seconds, which compensates for temporary network issues but is also still within the range of a human. 网络超时通常在30到120秒之间,这可以补偿临时的网络问题,但仍在人工范围内。 A user's attention typically drifts after a few hundred milliseconds, so you'll want to keep your response time well under one second.
用户的注意力通常会在几百毫秒后漂移,因此您希望将响应时间保持在一秒以内。 However, it's not unusual to have to wait for a website to load for a few seconds.
但是,等待网站加载几秒钟并不罕见。
If you need to tweak such a critical parameter to find the best setting, the pragmatic way to do that is to set it to some initial value, get some measurement tools in place to monitor throughput and response rates, and initiate a stress test that simulates expected production level load. 如果您需要调整这样的关键参数以找到最佳设置,那么务实的方法是将其设置为某个初始值,安装一些测量工具以监视吞吐量和响应率,并启动模拟的压力测试预期的生产水平负荷。 Then you'll see your error and response rates go up or down and can tweak the setting until you have found a value that satisfies your desired constraints.
然后,您将看到错误和响应率上升或下降,并且可以调整设置,直到找到满足所需约束的值为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.