File tree Expand file tree Collapse file tree 5 files changed +48
-3
lines changed
Expand file tree Collapse file tree 5 files changed +48
-3
lines changed Original file line number Diff line number Diff line change 11FROM node:18-alpine as builder
22WORKDIR /app
33COPY indexer yarn.lock ./
4+ # Install build dependencies for native modules like heapdump
5+ RUN apk add --no-cache python3 make g++
46RUN rm -rf node_modules && yarn install --frozen-lockfile
57RUN npx graphql-codegen
68RUN yarn build
79
810FROM node:18-alpine
911WORKDIR /app
1012COPY 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++
1115RUN yarn install --frozen-lockfile
1216COPY --from=builder /app/dist ./dist
1317COPY --from=builder /app/src/config/global-bundle.pem ./dist/config/global-bundle.pem
1418COPY --from=builder /app/src/kadena-server/config/schema.graphql ./dist/kadena-server/config/schema.graphql
1519COPY --from=builder /app/src/circulating-coins/ ./dist/circulating-coins/
20+ # Create snapshots directory for heap dumps
21+ RUN mkdir -p /snapshots
1622EXPOSE 3001
1723
1824ARG INDEXER_MODE_PARAM
Original file line number Diff line number Diff line change 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" ,
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" ,
Original file line number Diff line number Diff 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 */
5148export 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 ,
Original file line number Diff line number Diff line change 1919 */
2020
2121import './plugins/instrument' ;
22+ import heapdump from 'heapdump' ;
2223import { ApolloServer , ApolloServerPlugin } from '@apollo/server' ;
2324import { expressMiddleware } from '@apollo/server/express4' ;
2425import { 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 ) ) ;
Original file line number Diff line number Diff line change 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+
53485360html-escaper@^2.0.0 :
53495361 version "2.0.2"
53505362 resolved "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz"
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+
66986715napi-wasm@^1.1.0 :
66996716 version "1.1.0"
67006717 resolved "https://registry.npmjs.org/napi-wasm/-/napi-wasm-1.1.0.tgz"
You can’t perform that action at this time.
0 commit comments