-
Type: Task
-
Resolution: Done
-
Priority: Critical - P2
-
None
-
Affects Version/s: 1.9.1
-
Component/s: Connectivity
When we set a timeout option it is ignored if the machine is turned off or not accessible. We use MongoDB for caching and have some fallback logic in case the DB is not accessible, but in this case the driver ignores the timeout value and needs at least 20 seconds to throw an exception.
Here is a sample code. We used the IP of a turned off machine for the second test, you may also try with a non-existing machine:
I've also attached the .cs file we used to demonstrate the issue.
static void Main(string[] args) { var stopwatch = new Stopwatch(); stopwatch.Start(); var settings = new MongoClientSettings { Server = new MongoServerAddress("localhost", 27017), ConnectTimeout = TimeSpan.FromSeconds(2), }; var client = new MongoClient(settings); try { client.GetServer().Connect(); } catch (Exception e) { Console.WriteLine(e); } stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed); // Non-existing machine stopwatch.Reset(); stopwatch.Start(); settings = new MongoClientSettings { Server = new MongoServerAddress("XXX.XXX.XXX.XX", 27017), ConnectTimeout = TimeSpan.FromSeconds(2), }; client = new MongoClient(settings); try { client.GetServer().Connect(); } catch (Exception e) { Console.WriteLine(e); } stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed); }
- is related to
-
CSHARP-1231 Expose Server Selection Timeout via Connection String and MongoClientSettings
- Closed