diff options
author | ADAM David Alan Martin <adam.martin@10gen.com> | 2017-10-31 15:49:49 -0400 |
---|---|---|
committer | ADAM David Alan Martin <adam.martin@10gen.com> | 2017-10-31 15:49:49 -0400 |
commit | 400b86d2963b30730cccfe7ae6829e6101998ac8 (patch) | |
tree | 2741ecc5a785e60afc80e4d76ec1fa9f15144b5d /src/mongo/client/connection_string_connect.cpp | |
parent | 2a8818d4fd7b833b82997c9996aca9eb14471f09 (diff) | |
download | mongo-400b86d2963b30730cccfe7ae6829e6101998ac8.tar.gz |
SERVER-31061 Add `mongo+srv://` support for URIs.
The shell now supports parsing and handling `mongo+srv://` style URIs,
as part of the DNS Seedlist support. These URIs require DNS SRV and
TXT record lookups, for extra configuration options. The shell also
supports a (limited) form of connection-failover -- when initially
connecting to a non-replica-set cluster, the shell will try each
host listed, in order, until a connection can be established.
Diffstat (limited to 'src/mongo/client/connection_string_connect.cpp')
-rw-r--r-- | src/mongo/client/connection_string_connect.cpp | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/mongo/client/connection_string_connect.cpp b/src/mongo/client/connection_string_connect.cpp index e4c99dd37e1..d824fa6be78 100644 --- a/src/mongo/client/connection_string_connect.cpp +++ b/src/mongo/client/connection_string_connect.cpp @@ -46,10 +46,10 @@ namespace mongo { stdx::mutex ConnectionString::_connectHookMutex; ConnectionString::ConnectionHook* ConnectionString::_connectHook = NULL; -DBClientBase* ConnectionString::connect(StringData applicationName, - std::string& errmsg, - double socketTimeout, - const MongoURI* uri) const { +std::unique_ptr<DBClientBase> ConnectionString::connect(StringData applicationName, + std::string& errmsg, + double socketTimeout, + const MongoURI* uri) const { MongoURI newURI{}; if (uri) { newURI = *uri; @@ -57,15 +57,18 @@ DBClientBase* ConnectionString::connect(StringData applicationName, switch (_type) { case MASTER: { - auto c = stdx::make_unique<DBClientConnection>(true, 0, std::move(newURI)); - - c->setSoTimeout(socketTimeout); - LOG(1) << "creating new connection to:" << _servers[0]; - if (!c->connect(_servers[0], applicationName, errmsg)) { - return 0; + for (const auto& server : _servers) { + auto c = stdx::make_unique<DBClientConnection>(true, 0, newURI); + + c->setSoTimeout(socketTimeout); + LOG(1) << "creating new connection to:" << server; + if (!c->connect(server, applicationName, errmsg)) { + continue; + } + LOG(1) << "connected connection!"; + return std::move(c); } - LOG(1) << "connected connection!"; - return c.release(); + return nullptr; } case SET: { @@ -74,9 +77,9 @@ DBClientBase* ConnectionString::connect(StringData applicationName, if (!set->connect()) { errmsg = "connect failed to replica set "; errmsg += toString(); - return 0; + return nullptr; } - return set.release(); + return std::move(set); } case CUSTOM: { @@ -91,7 +94,7 @@ DBClientBase* ConnectionString::connect(StringData applicationName, _connectHook); // Double-checked lock, since this will never be active during normal operation - DBClientBase* replacementConn = _connectHook->connect(*this, errmsg, socketTimeout); + auto replacementConn = _connectHook->connect(*this, errmsg, socketTimeout); log() << "replacing connection to " << this->toString() << " with " << (replacementConn ? replacementConn->getServerAddress() : "(empty)"); |