Lazy-load CLS module
As soon as CLS module is loaded, the instrumentation/patching of async-listener is fired. This may cause stack overflows due to promise instrumentation. By loading CLS module lazily (only when used for real), we avoid this kind of problems in applications that are not using current-context at all.
This commit is contained in:
parent
867a7c3616
commit
1f214bf3ba
|
@ -5,9 +5,21 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var cls = require('continuation-local-storage');
|
|
||||||
var domain = require('domain');
|
var domain = require('domain');
|
||||||
|
|
||||||
|
|
||||||
|
// Require CLS only when using the current context feature.
|
||||||
|
// As soon as this require is done, all the instrumentation/patching
|
||||||
|
// of async-listener is fired which is not ideal.
|
||||||
|
//
|
||||||
|
// Some users observed stack overflows due to promise instrumentation
|
||||||
|
// and other people have seen similar things:
|
||||||
|
// https://github.com/othiym23/async-listener/issues/57
|
||||||
|
// It all goes away when instrumentation is disabled.
|
||||||
|
var cls = function() {
|
||||||
|
return require('continuation-local-storage');
|
||||||
|
};
|
||||||
|
|
||||||
var LoopBackContext = module.exports;
|
var LoopBackContext = module.exports;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,7 +86,7 @@ LoopBackContext.createContext = function(scopeName) {
|
||||||
process.context = process.context || {};
|
process.context = process.context || {};
|
||||||
var ns = process.context[scopeName];
|
var ns = process.context[scopeName];
|
||||||
if (!ns) {
|
if (!ns) {
|
||||||
ns = cls.createNamespace(scopeName);
|
ns = cls().createNamespace(scopeName);
|
||||||
process.context[scopeName] = ns;
|
process.context[scopeName] = ns;
|
||||||
// Set up LoopBackContext.getCurrentContext()
|
// Set up LoopBackContext.getCurrentContext()
|
||||||
LoopBackContext.getCurrentContext = function() {
|
LoopBackContext.getCurrentContext = function() {
|
||||||
|
|
Loading…
Reference in New Issue