@sentio/runtime 2.58.0-rc.7 → 2.58.0-rc.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentio/runtime",
3
- "version": "2.58.0-rc.7",
3
+ "version": "2.58.0-rc.8",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "exports": {
@@ -102,6 +102,7 @@ if (options['start-action-server']) {
102
102
  }
103
103
 
104
104
  const metricsPort = 4040
105
+
105
106
  const httpServer = http
106
107
  .createServer(async function (req, res) {
107
108
  if (req.url) {
@@ -115,17 +116,7 @@ const httpServer = http
115
116
  case '/heap': {
116
117
  try {
117
118
  const file = '/tmp/' + Date.now() + '.heapsnapshot'
118
- const session = new Session()
119
-
120
- const fd = fs.openSync(file, 'w')
121
- session.connect()
122
- session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
123
- fs.writeSync(fd, m.params.chunk)
124
- })
125
-
126
- await session.post('HeapProfiler.takeHeapSnapshot')
127
- session.disconnect()
128
- fs.closeSync(fd)
119
+ await dumpHeap(file)
129
120
  // send the file
130
121
  const readStream = fs.createReadStream(file)
131
122
  res.writeHead(200, { 'Content-Type': 'application/json' })
@@ -192,6 +183,41 @@ process
192
183
  // shutdownServers(1)
193
184
  })
194
185
 
186
+ if (process.env['OOM_DUMP_MEMORY_SIZE_GB']) {
187
+ let dumping = false
188
+ const memorySize = parseInt(process.env['OOM_DUMP_MEMORY_SIZE_GB'], 10)
189
+ console.log('heap dumping is enabled, limit set to ', memorySize, 'gb')
190
+ setInterval(async () => {
191
+ const mem = process.memoryUsage()
192
+ // if memory usage is greater this size, dump heap and exit
193
+ if (mem.heapTotal > memorySize * 1024 * 1024 * 1024 && !dumping) {
194
+ const file = '/tmp/' + Date.now() + '.heapsnapshot'
195
+ dumping = true
196
+ await dumpHeap(file)
197
+ // force exit and keep pod running
198
+ process.exit(11)
199
+ }
200
+ }, 1000 * 60)
201
+ }
202
+
203
+ async function dumpHeap(file: string) {
204
+ console.log('Heap dumping to', file)
205
+ const session = new Session()
206
+ const fd = fs.openSync(file, 'w')
207
+ try {
208
+ session.connect()
209
+ session.on('HeapProfiler.addHeapSnapshotChunk', (m) => {
210
+ fs.writeSync(fd, m.params.chunk)
211
+ })
212
+
213
+ await session.post('HeapProfiler.takeHeapSnapshot')
214
+ console.log('Heap dumped to', file)
215
+ } finally {
216
+ session.disconnect()
217
+ fs.closeSync(fd)
218
+ }
219
+ }
220
+
195
221
  function shutdownServers(exitCode: number) {
196
222
  server?.forceShutdown()
197
223
  console.log('RPC server shut down')