private MongoOptions getMongoOptions() {
MongoOptions options = new MongoOptions();
options.autoConnectRetry
=
autoConnectRetry;
options.connectionsPerHost
=
connectionsPerHost;
options.connectTimeout =
connectionTimeout;
options.socketTimeout = socketTimeout;
options.threadsAllowedToBlockForConnectionMultiplier
=
threadsAllowedToBlockForConnectionMultiplier;
return options;
}
Server
Selection Timeout
Server
selection timeout is the number of milliseconds the mongo driver will wait
to select a server for an operation before giving up and raising an error.
This
option was introduced in the newer version of next-generation Mongo drivers
(version 3.2.x+ in Java). For each type of operation and user preference, the
MongoClient selects the server using a selection algorithm to execute the
operation.
For a
write operation on a standalone server, there is only one server available
which gets selected. In a replica set or sharded clusters, there can be more
than one server which fills the user preference criteria for an operation.
Possible
scenarios where server selection timeout can happen include – if a network is
down or a primary node failure in a replica set.
Mongo
driver uses 30s as a default value of the server selection timeout.
Depending on the use case, we can increase or decrease this threshold.
Connection Timeout
Connection
timeout is the number of milliseconds the driver will wait before a new
connection attempt is aborted.
After
selection of the server, the client tries to establish a connection with the
server.
Depending
on network infrastructure and load on the server, the client may have to wait
for a connection establishment. Possible scenarios where connection timeout can
happen – the server gets shut down, network issues, wrong IP/DNS, port number
etc
The
default value of a connection timeout depends on the version and language of
the driver. Mongo Java & Ruby latest driver versions have a 10s default
timeout for connection establishments while the NodeJs driver has no timeout.
If the
timeout is too high, you risk stalling your application. If the timeout is too
low, you can give up too quickly. It is better to test with
different values to find the right timeout for your application.
SocketTimeout
Socket
timeout is the number of milliseconds a send or receive on a socket can
take before timeout.
After
establishing a connection with the server, the client sends a request to the
server and receives the response back using an already established connection.
Internally, the connection uses a socket to send the client request and
receives the response
Mongo
Java & Nodejs Driver have a default socket timeout of 0s which means
basically no timeout. While Ruby offers a 5s socket timeout. You don’t want to put a
limit on this timeout as different operations will take the variable time to
operate.
Further Exploration
MongoClient
Timeout options vary on the different versions and languages of Mongo drivers.
We encourage you to go through the documentation of the MongoClient class of
your driver to understand your default timeout options. We have provided some
sample code below to illustrate the timeout configuration in Java and Ruby.
MongoDB Java Driver
List<MongoCredential>
creds = new
ArrayList<MongoCredential>();
creds.add(MongoCredential.createCredential(username, DBname, password);
MongoClientOptions.Builder
optionsBuilder = MongoClientOptions.builder();
optionsBuilder.connectTimeout(CONNECTION_TIME_OUT_MS);
optionsBuilder.socketTimeout(SOCKET_TIME_OUT_MS);
optionsBuilder.serverSelectionTimeout(SERVER_SELECTION_TIMEOUT_MS);
MongoClientOptions
options = optionsBuilder.build();
Mongo
m = new MongoClient(new
ServerAddress(server , port), creds, options);
No comments:
Post a Comment