node-continuation-local-storage icon indicating copy to clipboard operation
node-continuation-local-storage copied to clipboard

createNamespace(sName) with existing name ...should throw?

Open osher opened this issue 9 years ago • 0 comments

Perhaps I do not understand the full intent of the module, but here is MHO.

I observed an error prone behavior:

> cls = require('continuation-local-storage');
{ getNamespace: [Function: get], 
...
> foo = cls.createNamespace('foo');
Namespace {
...
> foo1 = cls.createNamespace('foo');
Namespace {
...
> foo1 == foo2
false

from here on, foo1 is effectively a useless reference.

implications

If by any circumstances two different "space owners" chose the same name, the first of them to create his store is prompted to lose data, and the second may find there data he does not expect.

This is a behavior that would be very taunting to detect on dev-time.

My take on this

I suspect that in order to allow this behavior be discovered in development-time - we should do one of the following:

1. Assure namespaces never collide

throw an error when trying to create a space for a name in use. This will assure that namespaces never collide. Packages use cls.get and only the context file (the one ran with node) does cls.create.

We may consider supporting symbols as namespace identifiers. Using namespaces across packages will require them all a reference to the same symbol - however, that's something that is done deliberately and intentionally - so it's like developer taking responsibility.

2. Create rules namespaces can be shared safely

  1. createNamespace(name) and getNamespace(name) should do the same: if the space is there - it's returned, if not - it's created.

  2. namespace.set(k,v) should return a value that indicates if a value is overwritten or not.

     OR
    
  3. use namespace.const(k,v) - throws when set a second time.

  4. namespace.const(k,v,true) - the developer takes responsibility on replacing a constant)

    OR BOTH 1+2 AND 3+4

Thoughts?

osher avatar Dec 12 '16 08:12 osher