Reputation: 372
I have a lambda function that connects to a MongoDB instance, and am adding a connection pool reuse logic so that when a container is reused by lambda, the previous connection is reused instead of creating a new one.
In my database.ts
file, previously there was a single export const db = mongo.createConnection(..)
that was being used everywhere in the code, which created a new connection on every function invocation.
Now, as per online tutorials, I stored the actual connection variable in a global variable in the file and converted the export to a function which checks whether the above-mentioned connection object is not null
, and if so returns it without creating a new connection. If it is indeed null
, then it creates a connection and assigns the result of createConnection
to the variable and also returns it.
Of course, I needed to convert all the usages of db
to db()
, since it was a function now. Logs added in the file indicated that the connection was indeed being reused for all subsequent calls to db
after the first one.
Then, I converted db
into an IIFE and back to a normal export. I imagined that instead of having to go and replace all usages to a function call, it'd be better to call it in the file instead and then export it.
However now, what's happening is strange. The first invocation shows that a new connection is being created. (=> Creating a new database connection
) in the code. However on subsequent invocations, none of the log statements are printed. Ideally, shouldn't (=> Reusing an existing connection
) be printed on the console? I suppose the connection is still being reused since the first log does not appear, but why does this phenomenon occur?
I know it has something to do with how modules are require
d or cached and how AWS Lambda handles JS modules. Any further insight will be helpful.
let connection: mongoose.Connection | null = null;
let isPreviousConnectionAvailable = false;
export const db = (() => {
if (!isPreviousConnectionAvailable) {
log.debug('=> Creating a new database connection.');
connection = mongoose.createConnection(dbConnectionURL, { useNewUrlParser: true });
isPreviousConnectionAvailable = true;
} else {
log.debug('=> Reusing an existing database connection');
}
return connection;
})();
Upvotes: 3
Views: 224
Reputation: 341
The logs are not going to be printed. As it gets called the first time it gets imported and then subsequently you are accessing the same constant when the container is warm. It is the expected behavior.
Upvotes: 2