alfateam / node-cls Goto Github PK
View Code? Open in Web Editor NEWContinuation Local Storage based on async_hooks
Continuation Local Storage based on async_hooks
I run a server with koajs and mongoose and implemented node-cls
for accessing context data inside mongoose-middleware.
When experimenting with node-cls
, I encountered a bug:
Therefore I built a small koa-middleware:
export async function zoneMiddleware(ctx: Context, next) {
const name =
ctx.req.socket.remoteFamily +
'|' +
ctx.req.socket.remoteAddress +
'|' +
ctx.req.socket.remotePort;
const context = cls.create('request-context');
context.name = name;
await context.start();
console.log(cls.get('request-context').name);
await next();
cls.exit('request-context');
}
And the mongoose-middleware:
schema.pre(
'save',
async function (this: any, next: mongoose.HookNextFunction) {
console.log(getType(this) + ': pre save');
console.log(cls.get('request-context').name);
next();
}
);
The request-method then creates a new mongoose-document and saves it. This works as expected, but when I change the document and save it again, the context is lost:
async get() {
console.log(cls.get('request-context').name);
const kitty = new Kitten({ name: 'kitty' });
await kitty.save();
kitty.name = 'new Kitty';
kitty.test.test2.push('test');
kitty.test.test3.push('test2');
await kitty.save();
return { data: {} };
}
On a request I get the following log:
IPv6|::1|52799
IPv6|::1|52799
document: pre save
IPv6|::1|52799
document: pre save
TypeError: Cannot read property 'name' of undefined
I just wanted to report, that we have a linear memory leak in our application from this library. Especially in high throughput services (200k+/min async operations).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.