Giter Club home page Giter Club logo

node-cls's People

Contributors

lroal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-cls's Issues

Context getting lost

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

Memory leak with node-cls

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).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.