Is concurrent access to shared array a problem in Node.js

How is node handling events? One at the time, or concurrent?

I need to know, if there is going to be concurrent access to a shared array, like in the following example:

var ws          = require("./ws.js"),
    connections = [];

ws.createServer(function( socket ){

  // add to connection array
  socket.on('connect', function(){
    connections.push(socket);
  });

  // remove from connection array
  socket.on('close', function(){
    var i = connections.indexOf(socket);
    connections.splice(i,1);
  });

}).listen(8000);

When a client connects, its socket is pushed to the array. When the connection is closed, i want to remove it from the connections array.

But, in other languages this could lead to concurrency issues.

Fx. If two connections is closed at the same time:

  • Connection A finds its socket in index 4
  • Connection B finds its socket in index 5
  • Connection A deletes itself from index 4
  • Connection B deletes itself from index 5 (but it is now index 4)

Will this ever be a problem, or can i assume that only one callback is handled at a time?

6
задан Michael Andersen 23 September 2010 в 07:44
поделиться