简体   繁体   English

朝着同一服务器的CURLOPT_CONNECTTIMEOUT和CURLOPT_TIMEOUT的良好生产价值

[英]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 : 更新

  • API is brand new and only used by me API是全新的,仅供我使用
  • API is the key to get all the data. API是获取所有数据的关键。 If the API doesn't work, nothing works and I display an error message 如果API不起作用,则无任何作用,并且显示错误消息
  • API is run on each page to get the needed data 在每个页面上运行API以获取所需的数据

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: 考虑:

  • If you set the value too low, your code will fail if the API takes longer to respond for whatever reason. 如果将该值设置得太低,则由于任何原因API花费更长的时间响应,您的代码都会失败。
  • If the API takes longer to respond than ~4 seconds, what does that mean? 如果API的响应时间超过〜4秒,那是什么意思?
    • Is the API down entirely? API是否完全关闭? Then indeed you do not need to wait longer. 那么实际上您不需要等待更长的时间。
    • Is it overloaded? 它超载了吗? Then would it have responded within 5 second? 那它会在5秒钟内回复吗? 6? 6? 10? 10? How long will this overloaded state last? 这种过载状态将持续多长时间? Is it bad to compound it by overloading it more, or is it better to abort your code early and let the API recover? 通过重载更多代码来复合它是否不好,或者尽早中止代码并让API恢复会更好吗?
    • Is there a temporary network hickup? 是否有临时网络启动? Would that resolve if you made the timeout 5 seconds? 如果您将超时时间设置为5秒,是否可以解决? 10? 10? 20? 20? How reliable is the network typically? 网络通常有多可靠?
  • Would you prefer your app to be slow or dead if the API responds slowly for any of the above reasons? 如果API由于上述任何原因而响应缓慢,您希望应用程序运行缓慢还是死机
  • Can you guarantee a certain response time of the API, and if it doesn't keep that guarantee that means it's down? 您可以保证 API的一定响应时间吗?如果不能保证该响应时间,那就意味着它已经关闭了?
  • 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.

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