N
Neon2y ago
reduced-jade

Weird timeout on Cloudflare Workers

Hi. I was playing around with drizzle and the neon serverless driver over WebSockets and got an issue. When deployed to Cloudflare Workers using Vercel and Next, the first request passes and the second request timeouts 100% of the times. When deployed to Lambda, it always works. Here is the code: https://github.com/achaconm2001/neon-pool There are 2 routes: / => Main Page (edge) /test => Route (edge) /2 => Main Page (lambda) /test2 => Route (lambda)
GitHub
GitHub - achaconm2001/neon-pool
Contribute to achaconm2001/neon-pool development by creating an account on GitHub.
12 Replies
reduced-jade
reduced-jadeOP2y ago
Note: I didn't use the ws constructor because it causes weird errors. It seems to work fine without the ws constructor It seems every even request timeouts (on edge)
reduced-jade
reduced-jadeOP2y ago
Neon
Neon serverless driver - Neon Docs
The Neon serverless driver is a low latency Postgres driver for JavaScript and TypeScript that allows you to query data from serverless and edge environments over HTTP or WebSockets in place of TCP. T...
reduced-jade
reduced-jadeOP2y ago
Okay, I was able to fix the timeout issue with this code
// export const pool = new Pool({ connectionString: process.env.DATABASE_URL });
// export const db = drizzle(pool)

export const getDb = () => {
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
return drizzle(pool)
}
// export const pool = new Pool({ connectionString: process.env.DATABASE_URL });
// export const db = drizzle(pool)

export const getDb = () => {
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
return drizzle(pool)
}
Note 1: Every time I use the drizzle object, it creates a new Pool and returns it Note 2: I never call pool.close() Currently, it works, but may be I introduced a connection or memory leak I accept suggestions and comments :3
foreign-sapphire
foreign-sapphire2y ago
Hey, this does look leaky. I'd need to look into the code a little more later.
reduced-jade
reduced-jadeOP2y ago
GitHub
GitHub - achaconm2001/neon-pool
Contribute to achaconm2001/neon-pool development by creating an account on GitHub.
reduced-jade
reduced-jadeOP2y ago
I was exploring with the new using api from typescript 5.2
export const getDb = () => {
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
return {
db: drizzle(pool),
[Symbol.asyncDispose]: async () => await pool.end()
}
}
export const getDb = () => {
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
return {
db: drizzle(pool),
[Symbol.asyncDispose]: async () => await pool.end()
}
}
...
await using connection = getDb();
const result = await connection.db.execute(sql`SELECT 1 + 1`);
...
...
await using connection = getDb();
const result = await connection.db.execute(sql`SELECT 1 + 1`);
...
This will end the pool when the connection object goes out of scope It still have to create a new pool instance for all scopes where the db is used I'm still looking for a cleaner solution if you have anything in mind
genetic-orange
genetic-orange2y ago
@Gary, el Pingüino Artefacto what about
let db;
export const getDb = (env: Env) => {
if (!db) {
drizzle = drizzle(new Pool({ connectionString: env.DATABASE_URL }))
}
return db;
}
let db;
export const getDb = (env: Env) => {
if (!db) {
drizzle = drizzle(new Pool({ connectionString: env.DATABASE_URL }))
}
return db;
}
reduced-jade
reduced-jadeOP2y ago
This will cause the timeout of the even requests again Will work on Lambda but not ok Cloudflare Workers
reduced-jade
reduced-jadeOP2y ago
Neon
Neon serverless driver - Neon Docs
The Neon serverless driver is a low latency Postgres driver for JavaScript and TypeScript that allows you to query data from serverless and edge environments over HTTP or WebSockets in place of TCP. T...
reduced-jade
reduced-jadeOP2y ago
may be creating a new pool for each scope isn't that bad as I think, I did some benchmarks and the creation of the Pool is (almost) under < 1ms
foreign-sapphire
foreign-sapphire2y ago
Did you achieve something? @Gary, el Pingüino Artefacto @ShinyPokemon @schmime I must know if this causes a memory leak or drain my connection pool @Gary, el Pingüino Artefacto @ShinyPokemon @schmime
reduced-jade
reduced-jadeOP2y ago
you can test it yourself, i put all the code on git

Did you find this page helpful?