From 988b45ecdc5b9f17c0225b7c38752252a2184377 Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 12 Mar 2021 13:35:54 +0800 Subject: [PATCH 1/9] async_hooks supporting --- lib/command.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/command.js b/lib/command.js index 717115c82eb..d1ecde828e3 100644 --- a/lib/command.js +++ b/lib/command.js @@ -2,11 +2,34 @@ var betterStackTraces = /development/i.test(process.env.NODE_ENV) || /\bredis\b/i.test(process.env.NODE_DEBUG); -function Command (command, args, callback, call_on_write) { +var AsyncResource +var executionAsyncId + +try { + var asyncHooks = require("async_hooks"); + //asyncResource.runInAsyncScope added since node v9.6.0 + if (typeof asyncHooks.AsyncResource.prototype.runInAsyncScope === "function") { + AsyncResource = asyncHooks.AsyncResource; + executionAsyncId = asyncHooks.executionAsyncId; + } +} catch (e) { } + +function Command(command, args, callback, call_on_write) { this.command = command; this.args = args; this.buffer_args = false; - this.callback = callback; + + if (AsyncResource && typeof callback === 'function') { + var asyncResource = new AsyncResource('redis', executionAsyncId()) + var callbackWrapper = function (...args) { + asyncResource.runInAsyncScope(callback, this, ...args); + asyncResource.emitDestroy(); + } + this.callback = callbackWrapper; + } else { + this.callback = callback; + } + this.call_on_write = call_on_write; if (betterStackTraces) { this.error = new Error(); From 0912ca4ae710d59a80323aef0de249f36b7e61e7 Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 12 Mar 2021 14:28:39 +0800 Subject: [PATCH 2/9] for eslint: rules for es6 is not allowed --- lib/command.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/command.js b/lib/command.js index d1ecde828e3..b127971c492 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,30 +1,36 @@ 'use strict'; +var debug = require('./debug'); var betterStackTraces = /development/i.test(process.env.NODE_ENV) || /\bredis\b/i.test(process.env.NODE_DEBUG); -var AsyncResource -var executionAsyncId +var AsyncResource; +var executionAsyncId; try { - var asyncHooks = require("async_hooks"); - //asyncResource.runInAsyncScope added since node v9.6.0 - if (typeof asyncHooks.AsyncResource.prototype.runInAsyncScope === "function") { + var asyncHooks = require('async_hooks'); + if (typeof asyncHooks.AsyncResource.prototype.runInAsyncScope === 'function') { AsyncResource = asyncHooks.AsyncResource; executionAsyncId = asyncHooks.executionAsyncId; } -} catch (e) { } +} catch (e) { debug('not support'); } -function Command(command, args, callback, call_on_write) { +function Command (command, args, callback, call_on_write) { this.command = command; this.args = args; this.buffer_args = false; if (AsyncResource && typeof callback === 'function') { - var asyncResource = new AsyncResource('redis', executionAsyncId()) - var callbackWrapper = function (...args) { - asyncResource.runInAsyncScope(callback, this, ...args); + var asyncResource = new AsyncResource('redis', executionAsyncId()); + var callbackWrapper = function () { + // asyncResource.runInAsyncScope(callback, this, ...arguments); // eslint rules does not allow es6 + var params = [callback, this]; + if (arguments && arguments.length > 0) { + for (var i = 0; i < arguments.length; ++i) + params.push(arguments[i]); + } + asyncResource.runInAsyncScope.apply(asyncResource, params); asyncResource.emitDestroy(); - } + }; this.callback = callbackWrapper; } else { this.callback = callback; From f44c2080fe2cb383003deb34b685f7e2acb7242e Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 12 Mar 2021 14:31:12 +0800 Subject: [PATCH 3/9] for eslint: rules for es6 is not allowed --- lib/command.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/command.js b/lib/command.js index b127971c492..a36a20a1b42 100644 --- a/lib/command.js +++ b/lib/command.js @@ -14,7 +14,7 @@ try { } } catch (e) { debug('not support'); } -function Command (command, args, callback, call_on_write) { +function Command(command, args, callback, call_on_write) { this.command = command; this.args = args; this.buffer_args = false; @@ -22,7 +22,7 @@ function Command (command, args, callback, call_on_write) { if (AsyncResource && typeof callback === 'function') { var asyncResource = new AsyncResource('redis', executionAsyncId()); var callbackWrapper = function () { - // asyncResource.runInAsyncScope(callback, this, ...arguments); // eslint rules does not allow es6 + // asyncResource.runInAsyncScope(callback, this, ...arguments); // es6 rules not allow var params = [callback, this]; if (arguments && arguments.length > 0) { for (var i = 0; i < arguments.length; ++i) From baae5f78d41fb8d9b17db17fa3e36010608cd665 Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 12 Mar 2021 14:32:02 +0800 Subject: [PATCH 4/9] for eslint: rules for es6 is not allowed --- lib/command.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/command.js b/lib/command.js index a36a20a1b42..16f05451f2c 100644 --- a/lib/command.js +++ b/lib/command.js @@ -14,7 +14,7 @@ try { } } catch (e) { debug('not support'); } -function Command(command, args, callback, call_on_write) { +function Command (command, args, callback, call_on_write) { this.command = command; this.args = args; this.buffer_args = false; From 18a33800c81b6e1194d71bc9a00f1d5fbf873bdb Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 12 Mar 2021 21:10:21 +0800 Subject: [PATCH 5/9] add try catch for handle callback error --- lib/command.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/command.js b/lib/command.js index 16f05451f2c..0784b1d659c 100644 --- a/lib/command.js +++ b/lib/command.js @@ -22,14 +22,19 @@ function Command (command, args, callback, call_on_write) { if (AsyncResource && typeof callback === 'function') { var asyncResource = new AsyncResource('redis', executionAsyncId()); var callbackWrapper = function () { - // asyncResource.runInAsyncScope(callback, this, ...arguments); // es6 rules not allow - var params = [callback, this]; - if (arguments && arguments.length > 0) { - for (var i = 0; i < arguments.length; ++i) - params.push(arguments[i]); + try { + // asyncResource.runInAsyncScope(callback, this, ...arguments); // es6 rules not allow + var params = [callback, this]; + if (arguments && arguments.length > 0) { + for (var i = 0; i < arguments.length; ++i) + params.push(arguments[i]); + } + asyncResource.runInAsyncScope.apply(asyncResource, params); + asyncResource.emitDestroy(); + } catch (err) { + asyncResource.emitDestroy(); + throw err; } - asyncResource.runInAsyncScope.apply(asyncResource, params); - asyncResource.emitDestroy(); }; this.callback = callbackWrapper; } else { From 2face766af30db97527c6acdeccbfc42c5f210b0 Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 12 Mar 2021 21:15:22 +0800 Subject: [PATCH 6/9] add try catch for handle callback error --- lib/command.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/command.js b/lib/command.js index 0784b1d659c..56e25d1a2d3 100644 --- a/lib/command.js +++ b/lib/command.js @@ -5,23 +5,25 @@ var betterStackTraces = /development/i.test(process.env.NODE_ENV) || /\bredis\b/ var AsyncResource; var executionAsyncId; +var isSupported = false; try { var asyncHooks = require('async_hooks'); if (typeof asyncHooks.AsyncResource.prototype.runInAsyncScope === 'function') { AsyncResource = asyncHooks.AsyncResource; executionAsyncId = asyncHooks.executionAsyncId; + isSupported = true; } -} catch (e) { debug('not support'); } +} catch (e) { debug('async_hooks does not support'); } function Command (command, args, callback, call_on_write) { this.command = command; this.args = args; this.buffer_args = false; - if (AsyncResource && typeof callback === 'function') { + if (isSupported && typeof callback === 'function') { var asyncResource = new AsyncResource('redis', executionAsyncId()); - var callbackWrapper = function () { + this.callback = function () { try { // asyncResource.runInAsyncScope(callback, this, ...arguments); // es6 rules not allow var params = [callback, this]; @@ -36,7 +38,6 @@ function Command (command, args, callback, call_on_write) { throw err; } }; - this.callback = callbackWrapper; } else { this.callback = callback; } From 686d2e27c6a6ec7b363dcf316fa5dbcc69ff494d Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 12 Mar 2021 21:33:54 +0800 Subject: [PATCH 7/9] arguments vis always 'array' --- lib/command.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/command.js b/lib/command.js index 56e25d1a2d3..d2d4067af0c 100644 --- a/lib/command.js +++ b/lib/command.js @@ -20,14 +20,13 @@ function Command (command, args, callback, call_on_write) { this.command = command; this.args = args; this.buffer_args = false; - if (isSupported && typeof callback === 'function') { var asyncResource = new AsyncResource('redis', executionAsyncId()); this.callback = function () { try { // asyncResource.runInAsyncScope(callback, this, ...arguments); // es6 rules not allow var params = [callback, this]; - if (arguments && arguments.length > 0) { + if (arguments.length > 0) { for (var i = 0; i < arguments.length; ++i) params.push(arguments[i]); } @@ -41,7 +40,6 @@ function Command (command, args, callback, call_on_write) { } else { this.callback = callback; } - this.call_on_write = call_on_write; if (betterStackTraces) { this.error = new Error(); From 3c2aef0944cabfe12b070c01dee461242380a0d4 Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Fri, 12 Mar 2021 21:34:56 +0800 Subject: [PATCH 8/9] arguments vis always 'array' --- lib/command.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/command.js b/lib/command.js index d2d4067af0c..17e08d8e4be 100644 --- a/lib/command.js +++ b/lib/command.js @@ -24,12 +24,10 @@ function Command (command, args, callback, call_on_write) { var asyncResource = new AsyncResource('redis', executionAsyncId()); this.callback = function () { try { - // asyncResource.runInAsyncScope(callback, this, ...arguments); // es6 rules not allow + // asyncResource.runInAsyncScope(callback, this, ...arguments); var params = [callback, this]; - if (arguments.length > 0) { - for (var i = 0; i < arguments.length; ++i) - params.push(arguments[i]); - } + for (var i = 0; i < arguments.length; ++i) + params.push(arguments[i]); asyncResource.runInAsyncScope.apply(asyncResource, params); asyncResource.emitDestroy(); } catch (err) { From 90cb571cea9f2cc4418dc0affc14677880a9a468 Mon Sep 17 00:00:00 2001 From: Wu Jian Ping Date: Sat, 13 Mar 2021 11:10:12 +0800 Subject: [PATCH 9/9] for codeclimate --- lib/command.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/command.js b/lib/command.js index 17e08d8e4be..b5a21caa59a 100644 --- a/lib/command.js +++ b/lib/command.js @@ -25,14 +25,10 @@ function Command (command, args, callback, call_on_write) { this.callback = function () { try { // asyncResource.runInAsyncScope(callback, this, ...arguments); - var params = [callback, this]; - for (var i = 0; i < arguments.length; ++i) - params.push(arguments[i]); + var params = [callback, this].concat(Array.from(arguments)); asyncResource.runInAsyncScope.apply(asyncResource, params); + } finally { asyncResource.emitDestroy(); - } catch (err) { - asyncResource.emitDestroy(); - throw err; } }; } else {