@@ -7,7 +7,7 @@ const read = promisify(readFile);
77const common = require ( '../common.js' ) ;
88const {
99 createHook,
10- currentResource ,
10+ executionAsyncResource ,
1111 executionAsyncId
1212} = require ( 'async_hooks' ) ;
1313const { createServer } = require ( 'http' ) ;
@@ -18,7 +18,7 @@ const connections = 500;
1818const path = '/' ;
1919
2020const bench = common . createBenchmark ( main , {
21- type : [ 'current -resource' , 'destroy' ] ,
21+ type : [ 'async -resource' , 'destroy' ] ,
2222 method : [ 'callbacks' , 'async' ] ,
2323 n : [ 1e6 ]
2424} ) ;
@@ -27,7 +27,6 @@ function buildCurrentResource(getServe) {
2727 const server = createServer ( getServe ( getCLS , setCLS ) ) ;
2828 const hook = createHook ( { init } ) ;
2929 const cls = Symbol ( 'cls' ) ;
30- let closed = false ;
3130 hook . enable ( ) ;
3231
3332 return {
@@ -36,26 +35,18 @@ function buildCurrentResource(getServe) {
3635 } ;
3736
3837 function getCLS ( ) {
39- // we need to protect this, as once the hook is
40- // disabled currentResource will return null
41- if ( closed ) {
42- return ;
43- }
44-
45- const resource = currentResource ( ) ;
46- if ( ! resource [ cls ] ) {
38+ const resource = executionAsyncResource ( ) ;
39+ if ( resource === null || ! resource [ cls ] ) {
4740 return null ;
4841 }
4942 return resource [ cls ] . state ;
5043 }
5144
5245 function setCLS ( state ) {
53- // we need to protect this, as once the hook is
54- // disabled currentResource will return null
55- if ( closed ) {
46+ const resource = executionAsyncResource ( ) ;
47+ if ( resource === null ) {
5648 return ;
5749 }
58- const resource = currentResource ( ) ;
5950 if ( ! resource [ cls ] ) {
6051 resource [ cls ] = { state } ;
6152 } else {
@@ -64,16 +55,13 @@ function buildCurrentResource(getServe) {
6455 }
6556
6657 function init ( asyncId , type , triggerAsyncId , resource ) {
67- if ( type === 'TIMERWRAP' ) return ;
68-
69- var cr = currentResource ( ) ;
70- if ( cr ) {
58+ var cr = executionAsyncResource ( ) ;
59+ if ( cr !== null ) {
7160 resource [ cls ] = cr [ cls ] ;
7261 }
7362 }
7463
7564 function close ( ) {
76- closed = true ;
7765 hook . disable ( ) ;
7866 server . close ( ) ;
7967 }
@@ -101,8 +89,6 @@ function buildDestroy(getServe) {
10189 }
10290
10391 function init ( asyncId , type , triggerAsyncId , resource ) {
104- if ( type === 'TIMERWRAP' ) return ;
105-
10692 transactions . set ( asyncId , getCLS ( ) ) ;
10793 }
10894
@@ -139,7 +125,7 @@ function getServeCallbacks(getCLS, setCLS) {
139125}
140126
141127const types = {
142- 'current -resource' : buildCurrentResource ,
128+ 'async -resource' : buildCurrentResource ,
143129 'destroy' : buildDestroy
144130} ;
145131
0 commit comments