C#MongoDB驱动程序忽略超时选项 [英] C# MongoDB Driver Ignores timeout options

查看:536
本文介绍了C#MongoDB驱动程序忽略超时选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在为Mongo DB使用C#驱动程序(1.9.1).如果数据库不可访问,我们有一些后备逻辑需要运行,但是默认超时时间太长.我们尝试更改它,但是我们输入的值被忽略了.对于测试,我们使用的是无响应机器的IP.

We are using the C# driver (1.9.1) for Mongo DB. We have some fallback logic which needs to run if the DB is not accessible, however the default timeout is too long. We tried to change it but the values we put are getting ignored. For the tests we were using the IP of a non-responding machine.

我们尝试在连接字符串中设置超时时间:

We tried setting the timeout in the connection string:

 <add key="Mongo" value="mongodb://xxx.xxx.xxx.xxx:27017/?socketTimeoutMS=2000&amp;connectTimeoutMS=2000&amp;waitqueuetimeoutms=2000"/>

或通过代码:

var client = new MongoClient(new MongoClientSettings
{
    Server = new MongoServerAddress("xxx.xxx.xxx.xxx"),
    SocketTimeout = new TimeSpan(0, 0, 0, 2),
    WaitQueueTimeout = new TimeSpan(0, 0, 0, 2),
    ConnectTimeout = new TimeSpan(0, 0, 0, 2)
});

两次请求均在平均约20秒后超时.

Both time the requests timeout after an average of about 20 seconds.

设置超时选项的方式可能出问题了.

What could be wrong in the way we are setting the timeout options.

推荐答案

有一张JIRA票证 CSHARP -1018 来跟踪此问题.基本上,当无法访问计算机时,驱动程序将忽略超时选项.超时选项,如果计算机关闭或无法访问,则忽略该选项.

There is a JIRA ticket CSHARP-1018 to track this issue. Basically the driver ignores timeout option when machine is not accessible. Timeout option it is ignored if the machine is turned off or not accessible.

连接逻辑已在2.0中修复.目前它会尝试30秒,但是如果您需要更快的连接时间,可以将其配置为更小

Connection logic has been fixed in 2.0. It currently will try for 30 seconds, but that is configurable to something smaller if you need faster connection times

请参阅JIRA票证以跟踪此问题的进展.

Please refer to the JIRA ticket to follow progress on this issue.

请参阅发布到 CSHARP-1231 的变通办法,以了解如何将ServerSelectionTimeout设置为如果您希望在特定操作上使用更短的超时时间,则可以在当前2.0.0版本的驱动程序中进行设置.

See the workaround posted to CSHARP-1231 for a way that the ServerSelectionTimeout can be set in the current 2.0.0 version of the driver if you prefer that approach to using shorter timeouts on specific operations.

如果您使用的是新的2.0异步API,则可以使用取消令牌将自己的超时应用于整个操作.

If you are using the new 2.0 async API you can use a cancellation token to apply your own timeout to the overall operation.

因此,我将在先前的评论中推荐取消令牌方法.如果服务器选择超时时间短于完成选举所需的时间,则使用较短的服务器选择超时可能会导致副本集选举期间出现虚假异常.

So I would recommend the cancellation token approach in the previous comment. Using short server selection timeouts can result in spurious exceptions during replica set elections if the server selection timeout is shorter than the time it takes an election to complete.

您可以这样写:

var startTime = DateTime.UtcNow;
try
{
    using (var timeoutCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500)))
    {
        await collection.Find("{ _id : 1 }").ToListAsync(timeoutCancellationTokenSource.Token);
    }
}
catch (OperationCanceledException ex)
{
    var endTime = DateTime.UtcNow;
    var elapsed = endTime - startTime;
    Console.WriteLine("Operation was cancelled after {0} seconds.", elapsed.TotalSeconds);
}

在此示例中,即使ServerSelectionTimeout仍是默认值30秒,该特定操作也将仅在500毫秒后被取消(大约有时取消会花费更长的时间).

In this example, even though the ServerSelectionTimeout is still the default value of 30 seconds, this particular operation will be cancelled after only 500 milliseconds (approximately, cancellation can sometimes take slightly longer).

这篇关于C#MongoDB驱动程序忽略超时选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆