MetricsDAO / xyz

MetricsDAO XYZ Website
https://metricsdao.xyz
Apache License 2.0
12 stars 8 forks source link

Bug - Neon (Postgres) and Vercel connection #716

Open sebruiz opened 1 year ago

sebruiz commented 1 year ago

Occasionally we see errors like this in Vercel:

PrismaClientKnownRequestError: 
Invalid `prisma.user.findUnique()` invocation:

Can't reach database server at `ep-yellow-lab-282559-pooler.us-east-2.aws.neon.tech`:`5432`

Please make sure your database server is running at `ep-yellow-lab-282559-pooler.us-east-2.aws.neon.tech`:`5432`.
    at RequestHandler.handleRequestError (/var/task/node_modules/@prisma/client/runtime/index.js:30873:13)
    at RequestHandler.request (/var/task/node_modules/@prisma/client/runtime/index.js:30856:12)
    at PrismaClient._request (/var/task/node_modules/@prisma/client/runtime/index.js:31836:16)
    at getUser (/var/task/build/build-nodejs-eyJydW50aW1lIjoibm9kZWpzIn0.js:8604:14)
    at loader (/var/task/build/build-nodejs-eyJydW50aW1lIjoibm9kZWpzIn0.js:9046:14)
    at Object.callRouteLoaderRR (/var/task/node_modules/@remix-run/server-runtime/dist/data.js:41:16)
    at callLoaderOrAction (/var/task/node_modules/@remix-run/router/dist/router.cjs.js:3565:14)
    at async Promise.all (index 0)
    at loadRouteData (/var/task/node_modules/@remix-run/router/dist/router.cjs.js:3284:19)
    at queryImpl (/var/task/node_modules/@remix-run/router/dist/router.cjs.js:3115:20) {
  code: 'P1001',
  clientVersion: '4.5.0',
  meta: {
    database_host: 'ep-yellow-lab-282559-pooler.us-east-2.aws.neon.tech',
    database_port: 5432
  }
}

This might be from a "cold start" where Neon is set Idle instead of Active after a period of a no use. See:

https://neon.tech/docs/guides/prisma#connection-timeouts

Our connection string at time of this writing: postgres://<USER>:<PASS>@ep-yellow-lab-282559-pooler.us-east-2.aws.neon.tech/neondb?pgbouncer=true&pool_timeout=20

sebruiz commented 1 year ago

Official response from Neon:

Hi Sebastian​,

I am on the DevRel team over here at Neon. Thank you for reaching out.

This is indeed related to the cold start. This is something we're aware of and are actively working on. To address this issue, you can increase the maximum number of seconds to wait for a new connection to be opened or disable the timeout behavior for Prisma.

To do that, adjust your Neon connection string by adding a connect_timeout parameter. You can set this parameter to a high value or 0 to disable the timeout behavior

The connection string will have the following format DATABASE_URL= postgres://:@ep-yellow-lab-282559-pooler.us-east-2.aws.neon.tech/neondb?pgbouncer=true&pool_timeout=20&connect_timeout=10

This should fix the error you're running into. I'll make sure that this fix is reflected in the docs. Thank you for reporting!