In JavaScript, everything is much simpler. In Node, there are no threads, 
no such problems.

>From your question, especially "thread-local / request-local" I think you 
are talking about a server, maybe an http server, maybe another protocol. 
Either way, what you want is very simple in Node. For http, for example, in 
standard http module provided, every request is processed by a call that is 
given two two objects: request and response. Whatever you attach to these 
as properties/methods, are private to this request and not shared by other 
requests. You can also declare variables in your callback function and use 
them as parameters to other functions you will call during your request 
processing workflow.

In summary, only the variables that are declared in global scope (outside 
of body of any function) or others that are attached to those global-scope 
objects are shared between request callbacks. Other than these, everything 
you will declare inside your functions will be accessible only from inside 
that call.

You can always make local variables accessible globally by *providing paths 
to them*:

var requests = [];

http.Server(function(req, res) {
    // req and res are what you want, already provided. They are 
request-local.
    requests.push(req); // here we provide a way for others to access this 
private from outside.
});

JavaScript is a simple language. Its objects are flexible, powerful; and 
also very simple. Java programming is more about bureaucracy (things 
enforced by design) while JavaScript is all about socially acceptable 
behavior (things recommended by convention).

On Monday, July 2, 2012 2:45:54 AM UTC-4, Gaurav Vaish wrote:
>
> Hi, 
>
> What is the best way to implement a thread-local / request-local 
> storage? 
>
> Basically, I am looking at a way to implement per-request-singleton 
> object definition. 
>
> (ThreadLocal: 
> http://docs.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html) 
>
>
>
> -- 
> Happy Hacking, 
> Gaurav Vaish 
> www.m10v.com 
>

Reply via email to