Skip to content

Commit

Permalink
extract the connection code to a new method
Browse files Browse the repository at this point in the history
  • Loading branch information
gedaiu committed Sep 12, 2022
1 parent f0147b7 commit 9ffda97
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions mongodb/vibe/db/mongo/connection.d
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,17 @@ final class MongoConnection {
this(MongoClientSettings cfg)
{
m_settings = cfg;

// Now let's check for features that are not yet supported.
if(m_settings.hosts.length > 1)
logWarn("Multiple mongodb hosts are not yet supported. Using first one: %s:%s",
m_settings.hosts[0].name, m_settings.hosts[0].port);
}

void connect()
{
void connectToHost(MongoHost host) {
bool isTLS;

/*
* TODO: Connect to one of the specified hosts taking into consideration
* options such as connect timeouts and so on.
*/
try {
m_conn = connectTCP(m_settings.hosts[0].name, m_settings.hosts[0].port);
m_conn = connectTCP(host.name, host.port);
m_conn.tcpNoDelay = true;
if (m_settings.ssl) {
auto ctx = createTLSContext(TLSContextKind.client);
Expand All @@ -178,7 +172,7 @@ final class MongoConnection {
ctx.useTrustedCertificateFile(m_settings.sslCAFile);
}

m_stream = createTLSStream(m_conn, ctx, m_settings.hosts[0].name);
m_stream = createTLSStream(m_conn, ctx, host.name);
isTLS = true;
}
else {
Expand All @@ -187,7 +181,7 @@ final class MongoConnection {
m_outRange = streamOutputRange(m_stream);
}
catch (Exception e) {
throw new MongoDriverException(format("Failed to connect to MongoDB server at %s:%s.", m_settings.hosts[0].name, m_settings.hosts[0].port), __FILE__, __LINE__, e);
throw new MongoDriverException(format("Failed to connect to MongoDB server at %s:%s.", host.name, host.port), __FILE__, __LINE__, e);
}

m_allowReconnect = false;
Expand Down Expand Up @@ -274,6 +268,11 @@ final class MongoConnection {
}
}

void connect()
{
connectToHost(m_settings.hosts[0]);
}

void disconnect()
{
if (m_conn) {
Expand Down

0 comments on commit 9ffda97

Please sign in to comment.