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 12, 2022
1 parent 9ffda97 commit ce3d2dd
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion mongodb/vibe/db/mongo/connection.d
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,31 @@ final class MongoConnection {

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

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

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

auto split = m_description.primary.indexOf(":");
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 +769,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 ce3d2dd

Please sign in to comment.