Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

continuation-local-storage support #583

Closed
mdarveau opened this issue Apr 15, 2015 · 4 comments
Closed

continuation-local-storage support #583

mdarveau opened this issue Apr 15, 2015 · 4 comments

Comments

@mdarveau
Copy link

Hi,

I opened sequelize/sequelize#3509 in the Sequelize project and, long story short, the main issue is that they had to shim Bluebird to support continuation-local-storage.

Instead of shimming the global Bluebird lib, they shimmed their own instance. This was a design decision as discussed on the thread with @mickhansen and @janmeier.

You might be wondering why on earth I am discussing Sequelize and CLS issue in a Bluebird issue. Well, that discussion made me wonder why CLS support couldn't be backed in bluebird. CLS is so nice for use cases like transaction, log context, HTTP request storage, ...

API could be something like Bluebird.enableCLSNamespace(ns) and implementation inspired by https://github.com/TimBeyer/cls-bluebird/blob/master/shim.js but without having to resort to shimming.

It's totally fine with me if your think this is a stupid idea and/or not in the scope of this project. I just want to know if you'd be open to such feature.

@petkaantonov
Copy link
Owner

Why doesn't CLS use domains? They are already enabled in bluebird. In any case, only a generic mechanism like domains is supported, not a specific implementation like CLS.

@janmeier
Copy link

CLS is a general mechanism for storing local variables in callback / promise chains (advertised as thread-local storage for callback chains), whereas domains only support error handling as far as I know.

However, I understand the sentiment of not wanting to litter bluebird with implementation details about other libraries, and shimming in sequelize is not too much work :)

@petkaantonov
Copy link
Owner

CLS is a general mechanism for storing local variables in callback / promise chains (advertised as thread-local storage for callback chains), whereas domains only support error handling as far as I know.

That's not what I mean, domains are also used to implement libraries like CLS:

var d = domain.create();
d.run(function() {
     domain.active.data = {foo: 1};
     setTimeout(function() {
         domain.active.data.foo  // 1;
     }, 10);
});

setTimeout(function() {
    // Throws an error
    domain.active.data.foo
}, 10);

There is nothing generic about CLS, it just monkey patches async methods it knows about hence it's not working with bluebird. Domains work the same way but at least they are in the core so they are supported in bluebird:

var d = domain.create();
d.run(function() {
     domain.active.data = {foo: 1};
     Promise.delay(10).then(function() {
         domain.active.data.foo  // 1;
     });
});

Promise.delay(10).then(function() {
    // Rejects from the type error
    domain.active.data.foo
});

@mdarveau
Copy link
Author

I was not aware you could do that with domains. Thank you for your input!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants