typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
34.13k stars 6.29k forks source link

Typeorm connection timeout over multiple warm start AWS Lambda invocations #8924

Open rake146 opened 2 years ago

rake146 commented 2 years ago

I'm getting a timeout error for a lambda that has been warm for an extensive period of time. What is the protocol to ensure the connection is refreshed in AWS Lambda now that the connection manager has been deprecated? Aside from initialising the connection inside the handler, is there a better way to manage this?

Here is a snippet for the connection.

let connection = new DataSource({type: 'mysql', ...config}).initialize();

export const handler = async () => {
  try {
    const con = await connection;
    // do stuff with connection
  } catch (err) {
    console.error(err);
    (await connection).destroy();
    connection = new DataSource({type: 'mysql', ...config}).initialize()
  }
};

The catching & reassignment of the DataSource initialisation also seems to fail as subsequent invocations also hit the timeout

Appreciate any help

juane1000 commented 2 years ago

Seriously, I'm having this exact same problem with Firebase Cloud Functions. My error logs have spiked from single digits to 1k+ in a single day. Is there a way to handle the errors?

cesarmoralesonya commented 2 years ago

I'm having exactly the same problem with RDS MariaDB, despite of network connection is correctly configured.

Please we need a solution.

miloadam99 commented 1 year ago

Any updates?

B1tBandit commented 1 year ago

I'm experiencing the same issue in Lambda.

Lambda settings Runtime: nodejs16.x Architectures: x86_64 Timeout: 5s

Source

import {PostConfirmationTriggerEvent} from 'aws-lambda';
import {DataSource} from "typeorm"

export const postSignupConfirmHandler = async (event: PostConfirmationTriggerEvent): Promise<PostConfirmationTriggerEvent> => {
    if (event.triggerSource === "PostConfirmation_ConfirmSignUp" && event.request.userAttributes.email) {
        try {
            console.log("Declaring datasource");
            const AppDataSource = new DataSource({
                type: "postgres",
                host: process.env.DB_HOST,
                port: Number(process.env.DB_PORT),
                username: process.env.DB_USERNAME,
                password: process.env.DB_PASSWORD,
                database: process.env.DB_DATABASE,
                logging: false,
                synchronize: false,
                entities: ["entities/*.*"]
            });
            console.log("Datasource declared");

            console.log("Initializing datasource");
            const ConnectedDataSource = await AppDataSource.initialize()
            console.log("Datasource initialized");

            if (ConnectedDataSource) {
                console.log("Connected to DataSource")
                console.log("Data Source: ")
                console.log(ConnectedDataSource)
            } else {
                console.log("Data Source is undefined")
            }

            return (event)

        } catch (err) {
            console.log("Error in try block: " + err);
            return (event)
        }
    }

    else {
        console.log("Not a Post Confirmation Trigger");
        return(event)
    }
};

Test Event (Tested Remotely)

{
  "triggerSource": "PostConfirmation_ConfirmSignUp",
  "request": {
    "userAttributes": {
      "email": "person@email.com"
    }
  }
}

Logs

START RequestId: 6eded0e8-48fb-4547-ab5a-34a18ff7b79e Version: $LATEST
2023-01-02T19:15:00.216Z    6eded0e8-48fb-4547-ab5a-34a18ff7b79e    INFO    Declaring datasource
2023-01-02T19:15:00.377Z    6eded0e8-48fb-4547-ab5a-34a18ff7b79e    INFO    Datasource declared
2023-01-02T19:15:00.377Z    6eded0e8-48fb-4547-ab5a-34a18ff7b79e    INFO    Initializing datasource
2023-01-02T19:15:05.216Z 6eded0e8-48fb-4547-ab5a-34a18ff7b79e Task timed out after 5.01 seconds

Response

{
  "errorMessage": "2023-01-02T19:15:05.216Z 6eded0e8-48fb-4547-ab5a-34a18ff7b79e Task timed out after 5.01 seconds"
}
B1tBandit commented 1 year ago

This ended up being an issue with VPC settings.

I tested this after noticing the DataSource connection was timing out. I added a 4 second connection timeout to my typeorm datasource.

Modified DataSource

const AppDataSource = new DataSource({
    type: "postgres",
    host: process.env.DB_HOST,
    port: Number(process.env.DB_PORT),
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE,
    logging: false,
    synchronize: false,
    entities: ["entities/*.*"],
    extra: {
        connectionTimeoutMillis: 4000
    }
})
gaurav-sheoran commented 1 year ago

I am having similar issue. lambda invocation seems to just ignore datasource.initialize() and invocation finishes without ever connecting to database.

DataSource

console.log('creating new database connection');
  dataSource = new DataSource({
    type: "postgres",
    host: process.env.RDS_HOSTNAME,
    database: process.env.RDS_DATABASE,
    username: process.env.RDS_USERNAME,
    password: process.env.RDS_PASSWORD,
    port: 5432,
    connectTimeoutMS: 30000,
    useUTC: true,
    entities: ["entities/*.entity{.js, .ts}"],
    logging: false,
    synchronize: false,
    extra: {
      connectionTimeoutMillis: 40000,
    },
  });

  console.log('starting to initialize new connection');

  return await dataSource
    .initialize()
    .then(() => {
      console.log('new connection created');
      return dataSource.manager;
    })
    .catch((err: any) => {
      console.log('could not create connection ${err}');
      throw new Error(err);
    });

Logs

4:30:37 PM 2023-02-10T00:30:37.615Z      8068115e-8b8c-4af4-9cfd-8a57306490d1    INFO    creating new database connection
4:30:37 PM 2023-02-10T00:30:37.624Z      8068115e-8b8c-4af4-9cfd-8a57306490d1    INFO    starting to initialize new connection
4:30:37 PM END RequestId: 8068115e-8b8c-4af4-9cfd-8a57306490d1
4:30:37 PM REPORT RequestId: 8068115e-8b8c-4af4-9cfd-8a57306490d1        Duration: 27.36 ms  
thnam1410 commented 1 year ago

Any updates or solutions guys? Seem like calling .initialize will immediately crash the lambda thread.

AnshG714 commented 1 year ago

Any update?

rajasekar-k1 commented 1 year ago

I am also facing the same issue. Any update?