-
Notifications
You must be signed in to change notification settings - Fork 545
Configuring the Legacy Driver
In the legacy-0.9 development cycle, the mechanism by which the driver is configured was refactored. Note that these changes do not apply to releases on the 26compat branch, or releases pre-dating legacy-0.9.0.This page describes the new configuration mechanism and documents the various configuration options that may be used.
The mongo::client::initialize
function, found in file src/mongo/client/init.h
configures the driver and starts background threads critical to the functioning of the driver. The function has a single parameter receiving a mongo::client::Options
object. If no value is provided, a default constructed mongo::client::Options
object is used.
To configure the driver to use non-default parameters, construct a new mongo::client::Options
object and use its setter methods to configure the parameters of interest, and pass this object to mongo::client::initialize
.
This method must be called exactly once by your application. Invoke mongo::client::initialize
as early as possible in your application startup phase, before any additional threads have been created.
When it is time to stop using the client driver, you may terminate its background tasks and release resources by invoking the mongo::client::terminate
function, found in file src/mongo/client/init.h
. However, please note that by default, you do not need to do so, since the behavior of mongo::client::initialize
with a default constructed mongo::client::Options
object is to organize an automatic invocation of mongo::client::terminate
by way of atexit
.
The mongo::client::terminate
function takes a grace period argument, specified in milliseconds. The shutdown routine will give background tasks the selected grace period to terminate cleanly. If they do not do so, mongo::client::terminate
will return an error status. If the returned error status is ExceededTimeLimit
it is safe to retry the call to mongo::client::terminate
. Otherwise, a non-OK return status from mongo::client::terminate
represents a permanent failure to shut down the driver cleanly. Please see the documentation for mongo::client::terminate
for additional details.
If you would prefer to explicitly manage the lifetime of the driver and not rely on atexit
, you may pass a mongo::client::Options
object customized by invoking Options::setCallShutdownAtExit(false)
. It is then the responsibility of the application code to make an appropriate call to mongo::client::terminate
.
Configuration of the driver is managed through a new class, added in the legacy-0.9.0
release, called Options
. This class is hosted in the mongo::client
namespace, and is defined in the header mongo/client/options.h
.
By default, calling mongo::client::initialize()
with no parameters is equivalent to calling mongo::client::initialize(mongo::client::Options())
passing a default constructed mongo::client::Options
object.
Because the mongo::client::Options
class has setters returning a mongo::client::Options&
you can chain together options to easily configure them at the call site:
using mongo::client::initialize;
using mongo::client::Options;
// Configure the mongo C++ client driver, enabling SSL and setting
// the SSL Certificate Authority file to "mycafile".
Status status = initialize(
Options().setSSLMode(Options:kSSLModeRequired).setSSLCAFile("mycafile")
);
if (!status.isOK()) {
// deal with errors
} else {
// Driver is up in SSL mode.
}
- Type:
bool
- Default:
true
- Semantics: If this option is 'true', then a successful call to
mongo::client::initialize
will schedule a call tomongo::client::terminate
withatexit
. The call tomongo::client::terminate
will be made with the value ofmongo::client::Options::current::autoShutdownGracePeriodMillis
- Type:
int
, interpreted as milliseconds - Default: 250
- Semantics: If
mongo::client::initialize
scheduled a call tomongo::client::terminate
withatexit
, then that call tomongo::client::terminate
will use the valueOptions::autoShutdownGracePeriodMillis
when callingmongo::client::terminate
.
- Type:
int
, interpreted as milliseconds - Default: 15
- Semantics: TODO
- Type: [
Options::kSSLEnabled
|Options:kSSLDisabled
] - Default:
Options::kSSLDisabled
- Semantics: If set to
Options:kSSLEnabled
the driver will require SSL connections to all mongo servers. If disabled, it will not request SSL. Note that if the servers you are connected to are in SSL required mode, you may not be able to connect. This value is an enumeration so that we may later extend it with akSSLPreferred
option, but that is not currently implemented.
- Type:
bool
- Default:
false
- Semantics: If true, will attempt to use FIPS-140 validated crypto if supported by the crypto library currently in use.
- Type:
std::string
- Default:
""
- Semantics: This flag only has an effect if
Options::current::SSLMode
isOptions::kSSLRequired
. If set, it specifies a file containing the certificate authority file to use. See the MongoDB SSL documentation for additional information on the CA file.
- Type:
std::string
- Default:
""
- Semantics: This flag only has an effect if
Options::current::SSLMode
isOptions::kSSLRequired
. If set, it specifies a file containing the SSL PEM key file to use. See the MongoDB SSL documentation for additional information on the PEM key file.
- Type:
std::string
- Default:
""
- Semantics: This flag only has an effect if
Options::current::SSLMode
isOptions::kSSLRequired
, and is only meaningful if a PEM key file has been set withOptions::setSSLPEMKeyFile
. If set, it specifies the password to be used to decrypt the SSL PEM key file specified withOptions::setSSLPEMKeyFile
. See the MongoDB SSL documentation for additional information on the PEM key file password.
- Type:
std::string
- Default:
""
- Semantics: This flag only has an effect if
Options::current::SSLMode
isOptions::kSSLRequired
. If set, it specifies the file to use as the SSL certificate revocation list. See the MongoDB SSL documentation for additional information on the certificate revocation list file.
- Type:
bool
- Default:
false
- Semantics: This flag only has an effect if
Options::current::SSLMode
isOptions::kSSLRequired
. Setting this option totrue
suppresses validation of certificates. In other words, invalid certificates will be accepted.
- Type: `Options::LogAppenderFactory
- Default: none
- Semantics: Use
setLogAppenderFactory
if you want to configure a custom appender to listen to the driver's internally logged messages. Setting such a factory will enable logging and relay logged messages to whatever appender you provide a factory method for. An example of how to do this can be found here. You may only configure one appender through startup options.
- Type:
logger::LogSeverity
- Default: LogSeverity::Log()
- Semantics: When providing a custom log appender (see above) use this to set the minimum severity level of logged messages. Messages that are of less importance than the level you provide will not be logged.
- Type:
bool
- Default:
false
- Semantics: If enabled, the client library will run BSON validation on data returned from the server to ensure that the returned data is valid BSON. Note that there is a performance cost to doing so.
-
You must call
mongo::client::initialize
before using the driver. You may only callmongo::client::initialize
once. -
Configuration of the driver is global. You may access a
const
reference to the current global configuration state of the driver by callingmongo::client::Options::current
. If called before enteringmain
, the values returned by accessors of theOptions
object returned byOptions::current
are indeterminate. If called aftermain
but before callingmongo::client::initialize
, a default constructed instance of theOptions
class will be returned. If called aftermongo::client::initialize
, the value returned byOptions::current
will reflect any customizedOptions
instance passed tomongo::client::initialize
. -
Configuration of the driver is not synchronized, and you may only invoke
mongo::client::initialize
once. We strongly recommend that you callmongo::client::initialize
as early as possible inmain
or your application startup code, preferably before creating any additional threads.