Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix promise traces #50

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
[fix] add arbitrary limits to Trace graph to bound memory usage
  • Loading branch information
jbunton-atlassian authored and Andrew Bradley committed Oct 27, 2018
commit 2811217f5ba822e7394d6997e44b3167dffb30ea
28 changes: 28 additions & 0 deletions trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
const DEBUG = false;
const {debug} = DEBUG ? require('./debug') : {};

// Arbitrarily limit ourselves so we don't use up all memory on storing stack traces
const MAX_RELATED_TRACES = 10;
const MAX_RELATED_TRAVERSAL_DEPTH = 10;
const MAX_STACKS_TO_JOIN = 50;

const chain = require('stack-chain');
const asyncHook = require('async_hooks');

Expand Down Expand Up @@ -180,17 +185,40 @@ class Trace {
}

this.relatedTraces.push(relatedTrace);
this.removeOldestRelatedTraces();

for (const subRelatedTrace of relatedTrace.walk()) {
mergeStacks(subRelatedTrace.stacks, this.stacks);
subRelatedTrace.removeOldestStacks();
}
}

removeOldestRelatedTraces() {
if (this.relatedTraces.length < MAX_RELATED_TRACES) {
return;
}

this.relatedTraces.sort((a, b) => b.asyncId - b.asyncId);
this.relatedTraces.length = MAX_RELATED_TRACES
}

sortStacks() {
this.stacks.sort((a, b) => b.asyncId - a.asyncId);
}

removeOldestStacks() {
if (this.stacks.length < MAX_STACKS_TO_JOIN) {
return;
}

this.sortStacks();
this.stacks.length = MAX_STACKS_TO_JOIN;
}

walk(visited=[this], depth=0) {
if (depth > MAX_RELATED_TRAVERSAL_DEPTH) {
return;
}
for (const trace of this.relatedTraces) {
if (!visited.includes(trace)) {
visited.push(trace);
Expand Down