Skip to content

Commit

Permalink
add caching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Jun 1, 2023
1 parent c44c225 commit 5887f33
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,54 +449,65 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
}

return maybeCallback(async () => {
if (this.topology && this.topology.isConnected()) {
try {
if (this.connectionLock) return await this.connectionLock;

this.connectionLock = this._connect();
await this.connectionLock;
return this;
} finally {
this.connectionLock = undefined;
}
}, callback);
}

const options = this[kOptions];
private async _connect(): Promise<this> {
if (this.topology && this.topology.isConnected()) {
return this;
}

if (typeof options.srvHost === 'string') {
const hosts = await resolveSRVRecord(options);
const options = this[kOptions];

for (const [index, host] of hosts.entries()) {
options.hosts[index] = host;
}
if (typeof options.srvHost === 'string') {
const hosts = await resolveSRVRecord(options);

for (const [index, host] of hosts.entries()) {
options.hosts[index] = host;
}
}

const topology = new Topology(options.hosts, options);
// Events can be emitted before initialization is complete so we have to
// save the reference to the topology on the client ASAP if the event handlers need to access it
this.topology = topology;
topology.client = this;
const topology = new Topology(options.hosts, options);
// Events can be emitted before initialization is complete so we have to
// save the reference to the topology on the client ASAP if the event handlers need to access it
this.topology = topology;
topology.client = this;

topology.once(Topology.OPEN, () => this.emit('open', this));
topology.once(Topology.OPEN, () => this.emit('open', this));

for (const event of MONGO_CLIENT_EVENTS) {
topology.on(event, (...args: any[]): unknown => this.emit(event, ...(args as any)));
}
for (const event of MONGO_CLIENT_EVENTS) {
topology.on(event, (...args: any[]): unknown => this.emit(event, ...(args as any)));
}

const topologyConnect = async () => {
try {
await promisify(callback => topology.connect(options, callback))();
} catch (error) {
topology.close({ force: true });
throw error;
}
};

if (this.autoEncrypter) {
const initAutoEncrypter = promisify(callback => this.autoEncrypter?.init(callback));
await initAutoEncrypter();
await topologyConnect();
await options.encrypter.connectInternalClient();
} else {
await topologyConnect();
const topologyConnect = async () => {
try {
await promisify(callback => topology.connect(options, callback))();
} catch (error) {
topology.close({ force: true });
throw error;
}
};

return this;
}, callback);
}
if (this.autoEncrypter) {
const initAutoEncrypter = promisify(callback => this.autoEncrypter?.init(callback));
await initAutoEncrypter();
await topologyConnect();
await options.encrypter.connectInternalClient();
} else {
await topologyConnect();
}

return this;
}
/**
* Close the db and its underlying connections
*
Expand Down

0 comments on commit 5887f33

Please sign in to comment.