sequelize: Intermittent connection hanging with no errors using .authenticate()
Intermittent connection hanging with no errors using .authenticate()
I am using v5 with lambda functions and RDS postgres 10.6, node 12
Using both v4 and v5 and pg 7+ and 8+ I get an issue where sometimes the connection is never established I think. It seems to just hang in the authenticate() function where it is not returning an error or resolving. I have gone through the other issues where people were running into this issue on v4 so I updated to v5 but I still see the same problem. When it happens once it wont get out of this state until and new instance of the function is created. It is really hard to debug when there are no errors happening and my function will just timeout.
It makes no difference using context.callbackWaitsForEmptyEventLoop = false; or increasing or decreasing the idle time.
I never see - executing (default): SELECT 1+1 AS result. when this happens so I am not sure what is happening is it isn’t trying to see if it is connected.
How I am setting up my connection
let sequelize: Sequelize = null;
export const db = () => {
if (sequelize == null) {
console.log("new db connection");
return createInstanceDB();
} else {
console.log("old db connection");
return sequelize
.authenticate()
.then(() => sequelize)
.catch((err) => {
console.log("SEQ_ERROR", err);
return err
});
}
};
export const createInstanceDB = async () => {
const { database, username, password, host } = JSON.parse(process.env.DB);
// Open database connection
sequelize = new Sequelize(database, username, password, {
host: host,
dialect: "postgres",
pool: { max: 1, min: 0, idle: 2000},
logging: process.env.DB_DEBUG === "true" ? console.log : false,
});
// Initialize each model in the database
// setting up associations here
try {
await sequelize.authenticate();
console.log("Connection has been established successfully.");
return sequelize;
} catch (error) {
console.error("Unable to connect to the database:", error);
throw error;
}
};
Issue Template Checklist
Environment
Node 12.x PG 8.21.x Sequelize 5.21.10 AWS Lambda AWS RDS postgres
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
- I don’t know.
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don’t know how to start, I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 22 (8 by maintainers)
I just fixed it by setting the pg version to 8 and above.
I’m having a very similar issue to this, and I’ve followed the guide at https://sequelize.org/docs/v6/other-topics/aws-lambda/.
client.connect()
usingpg
directly, I get an instant success.authenticate()
on mysequelize
instance, it hangs.I added
DEBUG: sequelize*
and all it says during the function invocation when usingSequelize
is:The Lambda function then times out (15 seconds).
My dependencies:
Any ideas? @marcogrcr
EDIT: Hmm: After further investigation, everything works with a direct RDS instance, but fails with a RDS proxy…
With RDS proxy endpoint,
client.connect()
call succeeds usingpg
, but executing a query or callingsequelize.authenticate()
fails. With RDS instance endpoint, connect and query and sequelize all works. It seems my security groups were incorrect, but using RDS proxy,client.connect()
withpg
actually worked, it seems the RDS proxy did not attempt any connection on to the RDS instance at that point, only when actually authenticating and executing queries.