In my node.js
application I have a collection of client sockets as an array. When a communication error occurs, I simply call destroy
on the socket.
My question is: should I destroy the socket before or after removing it from the array? The documentation doesn't say much.
var clientSockets = []
var destroySocketBefore = function(socket) {
socket.destroy()
var socketIdx = clientSockets.indexOf(socket)
if (socketIdx > -1) {
clientSockets.splice(socketIdx, 1)
}
}
var destroySocketAfter = function(socket) {
var socketIdx = clientSockets.indexOf(socket)
if (socketIdx > -1) {
clientSockets.splice(socketIdx, 1)
}
socket.destroy()
}
-
In the case of
destroySocketBefore
, I am not sure if the socket will be found in the array if I destroy it before searching for it, so there is a possibility that array still incorporates invalid sockets in subsequent logic. -
In the case of
destroySocketAfter
, I am not sure if callingdestroy
on a socket that was removed from array will have the desired result. Is there a possibility that the system will delete the socket object after splicing the array, so sometimes I get to calldestroy
on anull
object.
I tested and it seems that both methods work as there is no difference between them, so I am not sure which method is the correct one.
via Alexandru Irimiea
No comments:
Post a Comment