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

Anonymous namespaces #52

Closed
binki opened this issue Dec 3, 2015 · 3 comments
Closed

Anonymous namespaces #52

binki opened this issue Dec 3, 2015 · 3 comments

Comments

@binki
Copy link

binki commented Dec 3, 2015

Hi, I’m looking into using Sequelize’s continuation-local-storage support. I don’t want to have to decide on a name for the namespace because I don’t like coming up with names and I would probably choose something stupid and generic which might conflict with code which I haven’t written.

I have no need to ever call getNamespace(). I can access the return value of createNamespace() from all code where the README would suggest I should call getNamespace(), so what’s the point of that function when I have an object reference available?

I would like to be able to something like the following to get automatic transaction binding through Promise chains whenever I use the sequelize object:

var clsNamespace = require('continuation-local-storage').createNamespace();
var Sequelize = require('sequelize');
Sequelize.cls = clsNamespace;
var sequelize = new Sequelize('sqlite:file.sqlite');

but this fails with AssertionError: namespace must be given a name! thrown by [email protected]. I don’t get it—what insurmountable technical obstacle to supporting anonymous namespaces requires me to use a name here? Could that be documented along with a workaround (i.e., a way reliably generate unique namespace names) if it isn’t going to be fixed?

@othiym23
Copy link
Owner

Since the point of CLS is to communicate across components in the same way that thread-local storage does (where the lookup is based on the thread itself), because it's not safe to assume that you're the only user of CLS within a given application, and because some applications of CLS do fancy things with the namespaces they create, the API requires that you give each namespace a name. It doesn't require it to be a good name – I've seen a lot of people simply using 'ns' or 'namespace' – but it does have to be there.

I'd be happy to land a patch that documents the motivation for the requirement, but I think adding a namespace name generator is superfluous, given that it's very rare that any application would need to generate more than a small handful of namespaces (which is good, because each one gets its own async-listener wrapping, which can pile up into some pretty significant overhead if you're not careful).

@binki
Copy link
Author

binki commented Feb 1, 2016

adding a namespace name generator is superfluous

That would be the wrong approach. The intention would be to be able to treat a CLS as anonymous/unnamed. You would only be able to access a particular CLS pool if you had a reference to its defining object. See the following behavior for comparing object references. I am able to determine if two handles are the same or not without having to name them. I’d like the same behavior from continuation-local-storage:

> var x = {};
undefined
> var y = {}
undefined
> x == y
false
> var z = x;
undefined
> z == x
true

Why must I come up with a string to identify a namespace? Naming things should only be necessary when trying to do something like IPC or when defining an interface that needs to be decoupled (such as look for a JSON key named X or pass a GET parameter Y). It shouldn’t be necessary when all parties needing to cooperate can exchange object references…

@binki
Copy link
Author

binki commented Sep 29, 2016

Just to clarify, I would implement this with Symbol or, if backwards compatibility is required, I would use something like a counter and a field such as anonymousId instead of name to distinguish named namespaces from anonymous ones.

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

No branches or pull requests

2 participants