Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.readFileSync is blocking the event loop #252

Closed
Jakobud opened this issue Dec 15, 2017 · 2 comments
Closed

fs.readFileSync is blocking the event loop #252

Jakobud opened this issue Dec 15, 2017 · 2 comments

Comments

@Jakobud
Copy link

@Jakobud Jakobud commented Dec 15, 2017

Here you are using fs.readFileSync instead of fs.readFile

https://github.com/motdotla/dotenv/blob/master/lib/main.js#L62

As I have understood how Node.js works in a website environment, using a Synchronous command temporary blocks other requests, even from other clients/visitors. So if you are loading dotenv on every single page load for a popular website, that means every time a page loads, then requests for every single other visitor to the website is temporarily blocked. I know it's a very short amount of time, but if you have 1000's of visitors hitting your website all at once, every single one of those requests having a Sync action in them could be a real problem.

http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/

Any thoughts on this? I'm not a Node.js expert and this is simply how I have always interpreted the Sync commands to work in a website (multi-user) project. If anyone could correct me or educate me if I'm wrong I would be interested in hearing it though.

And switching this over to an async statement would be very easy to do.

@Jakobud Jakobud changed the title Does using fs.readFileSync have a potential to block async activity? fs.readFileSync is blocking the event loop Dec 15, 2017
@maxbeatty

This comment has been minimized.

Copy link
Collaborator

@maxbeatty maxbeatty commented Dec 15, 2017

We purposely read the file synchronously so nothing else happens that may depend on an environment variable (e.g. start connecting to a database). You should only run dotenv.config once when your process begins and not during each request.

@maxbeatty maxbeatty closed this Dec 15, 2017
@Jakobud

This comment has been minimized.

Copy link
Author

@Jakobud Jakobud commented Dec 15, 2017

Ah okay I understand. That makes sense. Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.