简体   繁体   English

Bcryptjs - 使用同步与异步解决方案?

[英]Bcryptjs - using synchronous vs. asynchronous solutions?

I am trying to implement a simple login/authentication app and I am using bcryptjs in order to salt/hash the user's password before putting it in the database.我正在尝试实现一个简单的登录/身份验证应用程序,并且我正在使用 bcryptjs 以便在将用户密码放入数据库之前对其进行加盐/散列处理。 In the documentation it offers a synchronous way of hashing the password and an asynchronous way.在文档中,它提供了一种散列密码的同步方式和一种异步方式。 I guess my question is how do I know whether the asynchronous or synchronous version is right for my application and what should I be considering when deciding whether to implement a synchronous or asynchronous solution?我想我的问题是我如何知道异步或同步版本是否适合我的应用程序,以及在决定是实现同步还是异步解决方案时我应该考虑什么?

In general asynchronous tasks are preferred over synchronous to increase user interaction with the application.一般来说,异步任务比同步任务更受欢迎,以增加用户与应用程序的交互。 Synchronous tasks are executed in the order the line of code is written, hence blocks the thread assigned for processing.同步任务按照代码行的编写顺序执行,因此会阻塞分配用于处理的线程。 Since JavaScript is single threaded, the whole event loop is blocked.由于 JavaScript 是单线程的,因此整个事件循环都被阻塞了。

Same goes with bcrypt, as has been explained in their documentation :与 bcrypt 相同,正如他们的文档中所解释

Why is async mode recommended over sync mode?为什么推荐异步模式而不是同步模式?

If you are using bcrypt on a simple script, using the sync mode is perfectly fine.如果您在简单脚本上使用 bcrypt,则使用同步模式完全没问题。 However, if you are using bcrypt on a server, the async mode is recommended.但是,如果您在服务器上使用 bcrypt,则建议使用异步模式。 This is because the hashing done by bcrypt is CPU intensive, so the sync version will block the event loop and prevent your application from servicing any other inbound requests or events.这是因为 bcrypt 完成的散列是 CPU 密集型的,因此同步版本将阻止事件循环并阻止您的应用程序为任何其他入站请求或事件提供服务。 The async version uses a thread pool which does not block the main event loop.异步版本使用不会阻塞主事件循环的线程池。

I always choose asynchronous when it is available because it allows you to dispatch multiple tasks (practically) simultaneously.我总是在可用时选择异步,因为它允许您同时调度多个任务(实际上)。 However, in this case (authentication), it may seem like it doesn't matter (because authentication must be completed anyway before your other code should aquire further private data).但是,在这种情况下(身份验证),这似乎无关紧要(因为无论如何必须先完成身份验证然后您的其他代码才能获取更多私有数据)。

A question to ask is: What else do you need to do while authentication is pending?要问的一个问题是:在身份验证待处理期间,您还需要做什么? If you can think of something that could be done, while authentication is pending, that's where asynchronous really shines, because you'd be able to do that thing during the latency of the authentication.如果您能想到一些可以在身份验证未决时完成的事情,这就是异步真正发挥作用的地方,因为您可以在身份验证的延迟期间做那件事。

If you choose asynchronous, make sure you understand how promises work and I'd use await to make other portions of your script wait on that authentication-promise to resolve directly to a variable.如果您选择异步,请确保您了解promise 的工作原理,并且我会使用await使脚本的其他部分等待该身份验证承诺直接解析为变量。 If you don't know about these things, and are in hurry, only then would I settle for using synchronous.如果你不知道这些事情,而且很着急,那么我才会满足于使用同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM