-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathasync_hooks.js
142 lines (112 loc) · 3.09 KB
/
async_hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict'
const asyncHooks = require('async_hooks')
const Base = require('./base')
const platform = require('../platform')
const semver = require('semver')
// https://github.com/nodejs/node/issues/19859
const hasKeepAliveBug = !semver.satisfies(process.version, '^8.13 || >=10.14.2')
let singleton = null
class Scope extends Base {
constructor (config) {
if (singleton) return singleton
super()
singleton = this
this._trackAsyncScope = config.trackAsyncScope
this._current = null
this._spans = Object.create(null)
this._types = Object.create(null)
this._weaks = new WeakMap()
this._promises = [false]
this._stack = []
this._depth = 0
this._hook = asyncHooks.createHook({
init: this._init.bind(this),
before: this._before.bind(this),
after: this._after.bind(this),
destroy: this._destroy.bind(this),
promiseResolve: this._destroy.bind(this)
})
this._enabled = true
this._hook.enable()
}
_active () {
return this._current
}
_activate (span, callback) {
const active = this._active()
this._enter(span)
try {
return callback()
} finally {
this._exit(active)
}
}
_enter (span) {
this._depth++
this._stack[this._depth] = this._current
this._current = span
this._promises[this._depth] = false
}
_exit (span) {
this._trackAsyncScope && this._await(span)
this._current = span
this._stack[this._depth] = null
this._depth--
}
_exitNative () {
this._current = null
this._promises[0] = false
}
_await (span) {
if (!this._promises[this._depth]) return
this._enabled = false
this._awaitAsync(span)
this._enabled = true
}
// https://github.com/nodejs/node/issues/22360
async _awaitAsync (span) {
await {
then: (resolve) => {
this._current = span
resolve()
}
}
}
_initPromise () {
if (!this._promises[this._depth]) {
this._promises[this._depth] = true
this._await(this._current)
}
}
_init (asyncId, type, triggerAsyncId, resource) {
if (!this._enabled) return
this._spans[asyncId] = this._current
this._types[asyncId] = type
if (hasKeepAliveBug && (type === 'TCPWRAP' || type === 'HTTPPARSER')) {
this._destroy(this._weaks.get(resource))
this._weaks.set(resource, asyncId)
}
platform.metrics().increment('runtime.node.async.resources')
platform.metrics().increment('runtime.node.async.resources.by.type', `resource_type:${type}`)
if (this._trackAsyncScope && type === 'PROMISE') {
this._initPromise()
}
}
_before (asyncId) {
this._depth === 0 && this._exitNative()
this._enter(this._spans[asyncId])
}
_after () {
this._exit(this._stack[this._depth])
}
_destroy (asyncId) {
const type = this._types[asyncId]
if (type) {
platform.metrics().decrement('runtime.node.async.resources')
platform.metrics().decrement('runtime.node.async.resources.by.type', `resource_type:${type}`)
}
delete this._spans[asyncId]
delete this._types[asyncId]
}
}
module.exports = Scope