Reputation: 1436
My goal was to reduce the time it takes until the application stops to try connecting to a server. This is the solution I am now using: (It works, but I want to understand more in detail how)
MongoClientSettings clientSettings = new MongoClientSettings()
{
Server = new MongoServerAddress(host, port),
ClusterConfigurator = builder =>
{
//The "Normal" Timeout settings are for something different. This one here really is relevant when it is about
//how long it takes until we stop, when we cannot connect to the MongoDB Instance
//https://jira.mongodb.org/browse/CSHARP-1018, https://jira.mongodb.org/browse/CSHARP-1231
builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(2)));
}
};
I do not understand exactly what SocketTimeout
and ConnectTimeout
is then used for.
If I make those settings e.g. to 3 seconds, it actually would not make sense for the driver to wait longer then, since it is not expected that anything good can happen after the socket had a timeout?
My theory is that Connect and SocketTimeout affect how long it takes until the the connect to a single server waits, while serverSelectionTimeout is the timeout for the overall process? Is this true?
Upvotes: 2
Views: 949
Reputation: 101493
You might see here in ClusterRegistry.cs
that ConnectTimeout
is passed to TcpStreamSettings.ConnectTimeout
, while SocketTimeout
is passed to both TcpStreamSettings.ReadTimeout
and TcpStreamSettings.WriteTimeout
.
Then in TcpStreamFactory.cs
you see how those Read
and Write
timeouts are used: they are used as NetworkStream.ReadTimeout
and NetworkStream.WriteTimeout
, when creating stream to read\write data from\to TCP connection.
Now, if we go to documentation of NetworkStream.ReadTimeout we see there:
This property affects only synchronous reads performed by calling the Read method. This property does not affect asynchronous reads performed by calling the
BeginRead
method.
But in Mongo driver, those network streams are read asynchronously, which means those timeouts do nothing. Same story with NetworkStream.WriteTimeout.
So long story short: SocketTimeout
seems to have no effect at all and ConnectTimeout
on the other hand is used when doing TCP connection. How exactly it happens you can see in TcpStreamFactory.cs
.
Upvotes: 1