【问题标题】:PHP/MongoDB: find() is ignoring timeout settingPHP/MongoDB:find() 忽略超时设置
【发布时间】:2013-11-03 22:53:01
【问题描述】:

我的 PHP 脚本中的 find() 查询仍然会忽略尝试 mongodb 全局超时等。 我想要一个 findOne({...}) 或 find({...}) 查找并在超时前等待数据库服务器最多 20 毫秒。

如何确保 PHP 不将此设置用作软限制?它仍然被忽略并在 5 秒后处理答案。

这是 PHP mongo 驱动程序的错误吗?

例子:

MongoCursor::$timeout=20;
$nosql_server=new Mongo('mongodb://user:pw@'.implode(",",$arr_replicas).'',array("replicaSet" => "gmt","timeout"=>10)) OR troubles("too slow to connect");
$nosql_db=$nosql_server->selectDB('aDB');
$nosql_collection_mcol=$nosql_db->mcol;
$testFind=$nosql_collection_mcol->find(array('crit'=>123));
//If PHP considered the MongoCursor::$timeout, I'd expect the prev. line to be skipped or throwing a mongo/timeout exception if DB does not return the find result cursor ready within 20ms.
//However, I arrive with this line after seconds, without exception whenever the DB has some lock or delay, without skipping previous line.

【问题讨论】:

  • 您是在 php.ini 配置中还是在代码中设置超时?无论哪种方式,我们可以看看您使用什么代码来进行查找吗?
  • 我在脚本中设置了超时。已编辑:您现在可以找到示例代码。

标签: php mongodb timeout


【解决方案1】:

$timeout 的 PHP 文档中,对光标超时的解释如下:

导致获取结果的方法抛出一个 MongoCursorTimeoutException 如果查询花费的时间超过 指定的毫秒数。

我相信超时是指光标上执行的操作(例如getNext())。

【讨论】:

    【解决方案2】:

    不要这样做:

    MongoCursor::$timeout=20;
    

    那是调用一个静态方法,不会对你有任何好处。

    您需要意识到的是,在您的代码示例中,$testFind MongoCursor 对象。因此,在您提供的代码 sn-p 中,您应该在其他所有内容之后添加它以设置 $testFind MongoCursor 的超时:

    $testFind->timeout(100);
    

    注意:如果您想将 $testFind 作为一个数组处理,您需要这样做:

    $testFindArray = iterator_to_array($testFind);
    

    那个让我玩了一会儿。希望这对某人有所帮助。

    【讨论】:

      【解决方案3】:

      注意 readPreference 属性。可能的值是:

      MongoClient::RP_PRIMARY
      MongoClient::RP_PRIMARY_PREFERRED    
      MongoClient::RP_SECONDARY
      MongoClient::RP_SECONDARY_PREFERRED 
      MongoClient::RP_NEAREST
      

      【讨论】:

        猜你喜欢
        • 2015-09-22
        • 2020-06-10
        • 2011-04-11
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 2013-01-23
        相关资源
        最近更新 更多