Using A Blocking Long Sync Operation Will Pause All Users In Node.js?
Solution 1:
To understand the answer to your question, you really need to understand the node.js event loop. I highly recommend visiting that SO link and following a few of the links there.
If you must perform a synchronous operation, be aware that yes, a synchronous call will block all other requests on that process. If you want more than one client/remote to be served concurrently, then you'll definitely want to use the cluster module baked into node.js to spawn concurrent processes.
Also, if your synchronous operation is slow enough to impact your QoS, then it'd be a very smart idea to get familiar with process.nextTick
and prevent your processes from being completely stalled while waiting for a sync operation to complete.
Solution 2:
As far as I know, synchronous requests are not blocking all users, but before I do something like that, if I am not absolutely sure, I conduct a few tests. Using futures or other libraries, one can send synchronous requests, which is sub-optimal indeed, but if the developer does not have the rights/possibility to work with all the source-code, then it might be a necessary evil.
If this is done on the client-side (not this case, but let's clarify anyway), then a synchronous request from a browser looks like this:
funcion Foo(){
var request = new XMLHttpRequest();
request.open("GET", "http://server.com", false);
request.send();
function handleResponse(response) {
console.log(response);
}
handleResponse(request.responseText);
}
Post a Comment for "Using A Blocking Long Sync Operation Will Pause All Users In Node.js?"