-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add garbage collection to async resource map
- Loading branch information
1 parent
1f200cd
commit bbfcdf2
Showing
2 changed files
with
24 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
import async_hooks from 'node:async_hooks'; | ||
|
||
export const asyncResources = new Map(); | ||
// 50% of map limit | ||
export const GC_LIMIT = Math.pow(2, 23); | ||
|
||
export const AsyncResourceMap = new Map(); | ||
|
||
export const AsyncInterceptor = async_hooks.createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
logResourceCreation(asyncId, type, triggerAsyncId, resource); | ||
init(asyncId, type) { | ||
logResourceCreation(asyncId, type); | ||
}, | ||
}); | ||
|
||
function logResourceCreation(asyncId, type, triggerAsyncId, resource) { | ||
function logResourceCreation(asyncId, type) { | ||
const stack = (new Error()).stack.split('\n').slice(2).filter(line => { | ||
return !['AsyncHook.init', 'node:internal/async_hooks'].some(fn => line.includes(fn)); | ||
}).join('\n'); | ||
|
||
if (!asyncResources.has(stack)) { | ||
asyncResources.set(stack, { count: 0, types: new Set() }); | ||
if (AsyncResourceMap.size > GC_LIMIT) { | ||
console.log('Meteor Perf: Garbage collecting async resources'); | ||
garbageCollectAsyncResources(); | ||
} | ||
|
||
if (!AsyncResourceMap.has(stack)) { | ||
AsyncResourceMap.set(stack, { count: 0, types: new Set() }); | ||
} | ||
|
||
const resourceInfo = asyncResources.get(stack); | ||
const resourceInfo = AsyncResourceMap.get(stack); | ||
resourceInfo.count++; | ||
resourceInfo.types.add(type); | ||
} | ||
|
||
|
||
function garbageCollectAsyncResources() { | ||
AsyncResourceMap.forEach((info, stack) => { | ||
if (info.count <= 1) { | ||
AsyncResourceMap.delete(stack); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters