Skip to content

Commit 7924794

Browse files
committed
perf: share db, docker and auth singletons across duplicated bundles
The @dokploy/server modules get bundled multiple times per process (esbuild inline copy, compiled node_modules copy, and several Next.js chunks via transpilePackages), so every module-level side effect ran once per copy: up to 6 postgres pools, 6 dockerode clients and 6 better-auth instances in the server process, plus the repeated 'Using Docker socket' logs at boot. - db/index.ts: use the globalThis cache in production too (one pool per process) - constants/index.ts: cache the dockerode client on globalThis - lib/auth.ts: wrap betterAuth() in a factory and cache the instance - Dockerfile: exec node directly instead of leaving pnpm resident (~100MB RSS)
1 parent 3c114e2 commit 7924794

4 files changed

Lines changed: 391 additions & 379 deletions

File tree

‎Dockerfile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ EXPOSE 3000
6969
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=5 \
7070
CMD curl -fs http://localhost:3000/api/trpc/settings.health || exit 1
7171

72-
CMD ["sh", "-c", "pnpm run wait-for-postgres && exec pnpm start"]
72+
# Ejecutar node directamente: pnpm como wrapper queda residente (~100MB RSS)
73+
CMD ["sh", "-c", "node -r dotenv/config dist/wait-for-postgres.mjs && node -r dotenv/config dist/migration.mjs && exec node -r dotenv/config dist/server.mjs"]

‎packages/server/src/constants/index.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,17 @@ const getDockerConfig = (): Docker => {
8181
return new Docker({ ...versionOption });
8282
};
8383

84-
export const docker = getDockerConfig();
84+
// Igual que db: el módulo se evalúa una vez por copia bundleada, el cache
85+
// evita crear un cliente (y loguear la detección del socket) por cada una.
86+
const globalForDocker = globalThis as unknown as {
87+
docker?: Docker;
88+
};
89+
90+
if (!globalForDocker.docker) {
91+
globalForDocker.docker = getDockerConfig();
92+
}
93+
94+
export const docker = globalForDocker.docker;
8595

8696
export const paths = (isServer = false) => {
8797
const BASE_PATH =

‎packages/server/src/db/index.ts‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,19 @@ export * from "./schema";
99

1010
type Database = PostgresJsDatabase<typeof schema>;
1111

12-
/**
13-
* Evita problemas de redeclaración global en monorepos.
14-
* No usamos `declare global`.
15-
*/
12+
// Este módulo se evalúa varias veces por proceso (copias duplicadas por
13+
// esbuild y los chunks de Next); el cache en globalThis garantiza un solo
14+
// pool de conexiones por proceso.
1615
const globalForDb = globalThis as unknown as {
1716
db?: Database;
1817
};
1918

20-
let dbConnection: Database;
21-
22-
if (process.env.NODE_ENV === "production") {
23-
// En producción no usamos global cache
24-
dbConnection = drizzle(postgres(dbUrl), {
19+
if (!globalForDb.db) {
20+
globalForDb.db = drizzle(postgres(dbUrl), {
2521
schema,
2622
});
27-
} else {
28-
// En desarrollo reutilizamos conexión para evitar múltiples conexiones
29-
if (!globalForDb.db) {
30-
globalForDb.db = drizzle(postgres(dbUrl), {
31-
schema,
32-
});
33-
}
34-
35-
dbConnection = globalForDb.db;
3623
}
3724

38-
export const db: Database = dbConnection;
25+
export const db: Database = globalForDb.db;
3926

4027
export { dbUrl };

0 commit comments

Comments
 (0)