mongodb - When is blocking code acceptable in node.js? -
i know blocking code discouraged in node.js
because single-threaded. question asking whether or not blocking code acceptable in circumstances.
for example, if running express webserver requires mongodb connection, acceptable block event loop until database connection established? assuming pages served express require database query (which fail if mongodb not initialized).
another example application requires contents of configuration file before being initializing. there benefit in using fs.readfile
on fs.readfilesync
in case?
is there way work around this? wrapping code in callback or promise best way go? how different using blocking code in above examples?
it decide acceptable. , determining consequences of blocking ... on case-by-case basis. analysis take account:
- how occurs,
- how long event loop blocked, and
- the impact blocking in context have on usability1.
obviously, there ways avoid blocking, these tend add complexity application. really, need decide ... on case-by-case basis ... whether added complexity warranted.
bottom line: >>you<< need decide acceptable based on understanding of application , users.
1 - example, in game more acceptable block ui while switching "levels" during active play. or general web service, "once off" blocking while config file loaded or db connection established during webserver startup more acceptable if happened on every request.
Comments
Post a Comment