Skip to content

Commit

Permalink
Add basic support for replica set connections
Browse files Browse the repository at this point in the history
  • Loading branch information
gedaiu committed Sep 13, 2022
1 parent 9ffda97 commit c66b438
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion mongodb/vibe/db/mongo/connection.d
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,40 @@ final class MongoConnection {
authenticate();
break;
}

logInfo("Connected to: %s master=%s secondary=%s", m_description.me, m_description.ismaster, m_description.secondary);
}

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

foreach (host; m_settings.hosts) {
try {
connectToHost(host);
} catch(Exception ex) {
e = ex;
logError(e.msg);
}

if(!m_description.ismaster && m_description.secondary) {
logInfo("Connected to a secondary MongoDb node. The primary is: %s", m_description.primary);

auto split = m_description.primary.indexOf(":");

if(split != -1 && split < m_description.primary.length - 1) {
disconnect();
logInfo("Disconnected from the secondary MongoDb node.");

connectToHost(MongoHost(m_description.primary[0..split], m_description.primary[split+1..$].to!ushort));
return;
}
}
}

if(e !is null) {
throw e;
}
}

void disconnect()
Expand Down Expand Up @@ -745,6 +774,8 @@ struct ServerDescription
Nullable!int setVersion;
Nullable!BsonObjectID electionId;
string primary;
bool secondary;
bool ismaster;
string lastUpdateTime = "infinity ago";
Nullable!int logicalSessionTimeoutMinutes;

Expand Down

0 comments on commit c66b438

Please sign in to comment.