Skip to content

Commit 1ce6f2e

Browse files
authored
Merge pull request #504 from hack-a-chain-software/memory-leak
fix: removed memory leak by adding a correct configuration to pg pool
2 parents 64d6771 + d4683ce commit 1ce6f2e

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
FROM node:18-alpine as builder
22
WORKDIR /app
33
COPY indexer yarn.lock ./
4+
# Install build dependencies for native modules like heapdump
5+
RUN apk add --no-cache python3 make g++
46
RUN rm -rf node_modules && yarn install --frozen-lockfile
57
RUN npx graphql-codegen
68
RUN yarn build
79

810
FROM node:18-alpine
911
WORKDIR /app
1012
COPY indexer/package.json indexer/tsconfig.json yarn.lock ./
13+
# Install build dependencies for native modules like heapdump
14+
RUN apk add --no-cache python3 make g++
1115
RUN yarn install --frozen-lockfile
1216
COPY --from=builder /app/dist ./dist
1317
COPY --from=builder /app/src/config/global-bundle.pem ./dist/config/global-bundle.pem
1418
COPY --from=builder /app/src/kadena-server/config/schema.graphql ./dist/kadena-server/config/schema.graphql
1519
COPY --from=builder /app/src/circulating-coins/ ./dist/circulating-coins/
20+
# Create snapshots directory for heap dumps
21+
RUN mkdir -p /snapshots
1622
EXPOSE 3001
1723

1824
ARG INDEXER_MODE_PARAM

indexer/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@types/decimal.js": "^7.4.3",
5252
"@types/eventsource": "^1.1.15",
5353
"@types/express": "^4.17.21",
54+
"@types/heapdump": "^0.3.4",
5455
"@types/jest": "^29.5.12",
5556
"@types/mocha": "^10.0.7",
5657
"@types/node": "^20.11.17",
@@ -61,6 +62,7 @@
6162
"dotenv": "^16.4.4",
6263
"dotenv-cli": "^8.0.0",
6364
"eslint": "^8.56.0",
65+
"heapdump": "^0.3.15",
6466
"jest": "^29.7.0",
6567
"mocha": "^10.7.3",
6668
"module-alias": "^2.2.3",

indexer/src/config/database.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ if (isSslEnabled) {
4444
* This provides a lower-level database access mechanism than Sequelize.
4545
*
4646
* When SSL is enabled, it uses the global certificate bundle for secure connections.
47-
*
48-
* TODO: [OPTIMIZATION] Consider implementing connection pooling metrics to monitor performance
49-
* and adjust pool settings accordingly.
5047
*/
5148
export const rootPgPool = new Pool({
5249
connectionString: DB_CONNECTION,
50+
max: 20, // Max connections
51+
idleTimeoutMillis: 30000, // Close idle connections after 30s
52+
connectionTimeoutMillis: 10000, // Fail if can't get connection in 10s
53+
statement_timeout: 30000, // Cancel queries after 30s (PostgreSQL)
54+
query_timeout: 30000, // Client-side query timeout
5355
...(isSslEnabled && {
5456
ssl: {
5557
rejectUnauthorized: rejectUnauthorized,

indexer/src/kadena-server/server.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
import './plugins/instrument';
22+
import heapdump from 'heapdump';
2223
import { ApolloServer, ApolloServerPlugin } from '@apollo/server';
2324
import { expressMiddleware } from '@apollo/server/express4';
2425
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
@@ -510,6 +511,23 @@ export async function startGraphqlServer() {
510511
res.status(404).end();
511512
});
512513

514+
const logMemoryUsage = () => {
515+
const mem = process.memoryUsage();
516+
console.info(
517+
`[INFO][MEMORY] Heap: ${(mem.heapUsed / 1024 / 1024).toFixed(2)}MB / ${(mem.heapTotal / 1024 / 1024).toFixed(2)}MB | External: ${(mem.external / 1024 / 1024).toFixed(2)}MB | RSS: ${(mem.rss / 1024 / 1024).toFixed(2)}MB`,
518+
);
519+
520+
// Capture heap snapshot at 1GB memory threshold
521+
if (mem.heapUsed > 1 * 1024 * 1024 * 1024) {
522+
const filename = `/snapshots/indexer-heap-1GB-${Date.now()}.heapsnapshot`;
523+
console.warn(`[WARN][MEMORY] 1GB threshold reached! Capturing heap snapshot: ${filename}`);
524+
heapdump.writeSnapshot(filename);
525+
}
526+
};
527+
528+
// Start periodic memory monitoring every one hour
529+
setInterval(logMemoryUsage, 1000 * 60 * 60);
530+
513531
// Initialize cache and start the server
514532
await initCache(context);
515533
await new Promise<void>(resolve => httpServer.listen({ port: KADENA_GRAPHQL_API_PORT }, resolve));

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,6 +2663,11 @@
26632663
dependencies:
26642664
"@types/node" "*"
26652665

2666+
"@types/heapdump@^0.3.4":
2667+
version "0.3.4"
2668+
resolved "https://registry.yarnpkg.com/@types/heapdump/-/heapdump-0.3.4.tgz#59645b30165d832ea654449c8ffbed0c156ad60c"
2669+
integrity sha512-gTGn7W7i58F1yzvLZ5+hRE7M/VqgEzgccERJ9ZThC6qor5R8wo3oP7iiHvwgHIZzoqPF22ngu8mBagcqPWMVTg==
2670+
26662671
"@types/http-errors@*":
26672672
version "2.0.4"
26682673
resolved "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.4.tgz"
@@ -5345,6 +5350,13 @@ header-case@^2.0.4:
53455350
capital-case "^1.0.4"
53465351
tslib "^2.0.3"
53475352

5353+
heapdump@^0.3.15:
5354+
version "0.3.15"
5355+
resolved "https://registry.yarnpkg.com/heapdump/-/heapdump-0.3.15.tgz#631a8a2585588ea64778d8ec80a64c6c025f6a08"
5356+
integrity sha512-n8aSFscI9r3gfhOcAECAtXFaQ1uy4QSke6bnaL+iymYZ/dWs9cqDqHM+rALfsHUwukUbxsdlECZ0pKmJdQ/4OA==
5357+
dependencies:
5358+
nan "^2.13.2"
5359+
53485360
html-escaper@^2.0.0:
53495361
version "2.0.2"
53505362
resolved "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz"
@@ -6695,6 +6707,11 @@ [email protected]:
66956707
resolved "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz"
66966708
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==
66976709

6710+
nan@^2.13.2:
6711+
version "2.23.0"
6712+
resolved "https://registry.yarnpkg.com/nan/-/nan-2.23.0.tgz#24aa4ddffcc37613a2d2935b97683c1ec96093c6"
6713+
integrity sha512-1UxuyYGdoQHcGg87Lkqm3FzefucTa0NAiOcuRsDmysep3c1LVCRK2krrUDafMWtjSG04htvAmvg96+SDknOmgQ==
6714+
66986715
napi-wasm@^1.1.0:
66996716
version "1.1.0"
67006717
resolved "https://registry.npmjs.org/napi-wasm/-/napi-wasm-1.1.0.tgz"

0 commit comments

Comments
 (0)