N
Neon12mo ago
wise-white

Weird timeouts?

Hi, recently I started getting some timeouts in my personal account without any code changed.
4 Replies
wise-white
wise-whiteOP12mo ago
import { cache } from "react"
import { eq } from "drizzle-orm"
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"

import type { TenantId } from "@/config/branded"
import { getMainHttpDatabase } from "@/databases/main/connector"
import { mainTenantesTable } from "@/databases/main/schema"
import * as schema from "@/databases/tenant/schema"
import { neonApiClient } from "@/libs/neon-toolkit"

const getProjectConnectionString = async (projectId: string) => {
if (!process.env.VERCEL_ENV || process.env.VERCEL_ENV === "production") {
const project = await neonApiClient.GET("/projects/{project_id}/connection_uri", {
params: {
path: {
project_id: projectId,
},
query: {
database_name: "neondb",
role_name: "neondb_owner",
pooled: true,
},
},
})

return project.data?.uri
}

const branches = await neonApiClient.GET(`/projects/{project_id}/branches`, {
params: {
path: {
project_id: projectId,
},
},
})

const branch = branches.data?.branches.find((x) => x.name === process.env.VERCEL_BRANCH_URL)
if (!branch) {
throw new Error(`Branch ${process.env.VERCEL_BRANCH_URL} not found`)
}

const project = await neonApiClient.GET("/projects/{project_id}/connection_uri", {
params: {
path: {
project_id: projectId,
},
query: {
database_name: "neondb",
role_name: "neondb_owner",
pooled: true,
branch_id: branch.id,
},
},
})

return project.data?.uri
}
import { cache } from "react"
import { eq } from "drizzle-orm"
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"

import type { TenantId } from "@/config/branded"
import { getMainHttpDatabase } from "@/databases/main/connector"
import { mainTenantesTable } from "@/databases/main/schema"
import * as schema from "@/databases/tenant/schema"
import { neonApiClient } from "@/libs/neon-toolkit"

const getProjectConnectionString = async (projectId: string) => {
if (!process.env.VERCEL_ENV || process.env.VERCEL_ENV === "production") {
const project = await neonApiClient.GET("/projects/{project_id}/connection_uri", {
params: {
path: {
project_id: projectId,
},
query: {
database_name: "neondb",
role_name: "neondb_owner",
pooled: true,
},
},
})

return project.data?.uri
}

const branches = await neonApiClient.GET(`/projects/{project_id}/branches`, {
params: {
path: {
project_id: projectId,
},
},
})

const branch = branches.data?.branches.find((x) => x.name === process.env.VERCEL_BRANCH_URL)
if (!branch) {
throw new Error(`Branch ${process.env.VERCEL_BRANCH_URL} not found`)
}

const project = await neonApiClient.GET("/projects/{project_id}/connection_uri", {
params: {
path: {
project_id: projectId,
},
query: {
database_name: "neondb",
role_name: "neondb_owner",
pooled: true,
branch_id: branch.id,
},
},
})

return project.data?.uri
}
const _getTenantConnectionString = cache(async (tenantId: TenantId) => {
const connection = getMainHttpDatabase()
const [tenant] = await connection
.select({ neonDatabaseId: mainTenantesTable.neonDatabaseId })
.from(mainTenantesTable)
.where(eq(mainTenantesTable.id, tenantId))

if (!tenant?.neonDatabaseId) {
throw new Error(`Tenant ${tenantId} not found`)
}

const connectionString = await getProjectConnectionString(tenant.neonDatabaseId)
if (!connectionString) {
throw new Error(`Connection string not found for tenant ${tenantId}`)
}

return connectionString
})

const _getTenantDatabase = (connectionString: string) => {
const sql = postgres(connectionString)
return drizzle(sql, { schema })
}

export type TenantDatabase = ReturnType<typeof _getTenantDatabase>
const tenantDatabases = new Map<TenantId, TenantDatabase>()

export const getTenantHttpDatabase = async (tenantId: TenantId) => {
if (tenantDatabases.has(tenantId)) {
return tenantDatabases.get(tenantId)!
}

const connectionString = await _getTenantConnectionString(tenantId)
const db = _getTenantDatabase(connectionString)
tenantDatabases.set(tenantId, db)
return db
}

export const getTenantWsDatabase = async (tenantId: TenantId) => {
return await getTenantHttpDatabase(tenantId)
}
const _getTenantConnectionString = cache(async (tenantId: TenantId) => {
const connection = getMainHttpDatabase()
const [tenant] = await connection
.select({ neonDatabaseId: mainTenantesTable.neonDatabaseId })
.from(mainTenantesTable)
.where(eq(mainTenantesTable.id, tenantId))

if (!tenant?.neonDatabaseId) {
throw new Error(`Tenant ${tenantId} not found`)
}

const connectionString = await getProjectConnectionString(tenant.neonDatabaseId)
if (!connectionString) {
throw new Error(`Connection string not found for tenant ${tenantId}`)
}

return connectionString
})

const _getTenantDatabase = (connectionString: string) => {
const sql = postgres(connectionString)
return drizzle(sql, { schema })
}

export type TenantDatabase = ReturnType<typeof _getTenantDatabase>
const tenantDatabases = new Map<TenantId, TenantDatabase>()

export const getTenantHttpDatabase = async (tenantId: TenantId) => {
if (tenantDatabases.has(tenantId)) {
return tenantDatabases.get(tenantId)!
}

const connectionString = await _getTenantConnectionString(tenantId)
const db = _getTenantDatabase(connectionString)
tenantDatabases.set(tenantId, db)
return db
}

export const getTenantWsDatabase = async (tenantId: TenantId) => {
return await getTenantHttpDatabase(tenantId)
}
The timeout is consistent across all the projects, but it's only somethings Pressing F5 always fixed the issue
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"

import * as schema from "@/databases/main/schema"
import { env } from "@/env"

const db = drizzle(postgres(env.DATABASE_URL), { schema })

export const getMainHttpDatabase = () => {
return db
}

export const getMainWsDatabase = () => {
return db
}
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"

import * as schema from "@/databases/main/schema"
import { env } from "@/env"

const db = drizzle(postgres(env.DATABASE_URL), { schema })

export const getMainHttpDatabase = () => {
return db
}

export const getMainWsDatabase = () => {
return db
}
This is the main connector I'm not sure where to find my account ID, I can DM my email if needed Also, the framework is NextJS, but the issues occur also in local, not only on the Vercel platform
flat-fuchsia
flat-fuchsia12mo ago
@Gary, el Pingüino Artefacto Can you share a GitHub repository for me to reproduce?
flat-fuchsia
flat-fuchsia12mo ago
I have https://github.com/neondatabase/examples/blob/main/with-nextjs-drizzle-edge/drizzle.config.ts and https://github.com/neondatabase/examples/blob/main/with-nextjs-drizzle-edge/lib/drizzle.server.ts as the references, but I'd want to test your minimal reproducible so that I can make sure that many do not face the same problem again.
GitHub
examples/with-nextjs-drizzle-edge/drizzle.config.ts at main · neond...
Examples and code snippets demonstrating common ways of integrating Neon with various frameworks and languages. - neondatabase/examples
GitHub
examples/with-nextjs-drizzle-edge/lib/drizzle.server.ts at main · n...
Examples and code snippets demonstrating common ways of integrating Neon with various frameworks and languages. - neondatabase/examples
wise-white
wise-whiteOP12mo ago
It's actually fixed now, I think it was a Neon issue I literally didn't change anything And it fixed I can share my account if you want to inspect it

Did you find this page helpful?