I use this approach to store data in a global array where the HTTP server is located, where certain requests will be processed by the global array.
I am a little worried that I am facing problems with threads with certain operations - mainly push
and splice
. I assume that if one request requires me to .push()
over the array and delete the elements based on the conditional expression, while the other request calls me .push()
in the array in which I ran into problems. Can anyone confirm this?
I mainly write in C #, where even a simple increment is not thread safe (running 25 threads that I ++ do not guarantee that I == 25 after everything is said and done).
Update
I wrote 5 examples to demonstrate what I'm talking about. Test 1 and Test 3 work fine. Test 2 fails because ... what will usually be called threading problems (regardless of whether they are actual CPU threads or not). Tests 4 and 5 when running in parallel seem to work (which means they have no collision issues like test 2).
http://pastebin.com/HcJHTDFY
I use ApacheBench for testing, making 1000 concurrent requests.
This makes me think that Test 1 and Test 3 are working fine, because nodejs will not execute more than one instance of the app.get('/test3'...)
in the parallel javascript function in parallel (blocking?). Once you implement setInterval / setTimeout, it frees nodejs to execute another callback instance (non-blocking?).
I'm really just trying to figure out what the heck non-blocking I/O model
really means. Does this mean that βhey, you might not block using setTimeout and setInterval if you need non-blocking, otherwise we are going to block any other external-level functions from starting until we exhaust the function? I feel like this you need to know that I do not get into trouble thinking that I can implement something like / test 2 and be absolutely safe.
Also, if I try not to block my callbacks, should I really call setTimeout(code, 1)
? Or is there a better way?
source share