为什么我应该在pg_connect()函数中使用connect_timeout = 5?

时间:2013-09-09 17:21:43

标签: php postgresql

我最近得到了一条评论,我应该“在我的脚本中使用connect_timeout”。我去搜索实际意味着什么,我在用户评论php.net中找到了这些信息:

One thing is to remember, whenever trying to use pg_connect, add the timeout 
parameter with it

<?php 
$d=pg_connect('host=example.com user=pgsql dbname=postgres connect_timeout=5'); 
?>

所以,同样的信息“你应该使用它”,但没有解释为什么?所以,我希望有人可以为我澄清这个问题?

此外,是否应该设置一些“默认”connect_timeout,就像用户评论建议5一样?出于某种原因,pg_connect选择不当,我应该在每个connect_timeout来电中添加pg_connect,或者?

我在pgsql documentation进行了搜索,只是说:

connect_timeout
Maximum wait for connection, in seconds (write as a decimal integer string). Zero
or not specified means wait indefinitely. It is not recommended to use a timeout
of less than 2 seconds.

但我没有看到一个用例场景,坦率地说我以前从未使用它,所以,无论如何,对此的一些见解将是感激的。

1 个答案:

答案 0 :(得分:3)

对于网站,如果与数据库的连接挂起,则设置连接超时至少有两个原因:

  • 使用适当的错误消息正常降级并记录,而不是让用户等到无聊。

  • 不要让网络服务器累积进程并冒险达到其限制(例如apache的maxclients),以至于它根本不会提供任何请求。通常我们希望任何网络请求尽快完成。

相关问题