[英]Does setTimeout function affect the performance of Node.js application?
I have a request handler for ticket booking:我有一个订票请求处理程序:
route.post('/makeBooking', (req, res) => {
// Booking code
setTimeout(function () {
// Checks if the payment is made, if not then cancels the booking
}, 900000);
});
Now I've a route which makes a booking and if the payment is not made within 15 minutes the timeout function will cancel the booking.现在我有一条预订路线,如果未在 15 分钟内付款,超时功能将取消预订。
Will this function cause any performance related issues or memory leaks?此功能会导致任何与性能相关的问题或内存泄漏吗?
Will this function cause any performance related issues ...
此功能是否会导致任何与性能相关的问题...
No it won't, at least not in and by itself.不,它不会,至少它本身不会。 While
setTimeout
is waiting to invoke it's callback, it's non-blocking .虽然
setTimeout
正在等待调用它的回调,但它是非阻塞的。 The call is simply added to a queue.呼叫被简单地添加到队列中。 At some point in the future the callback fires and the call is removed from that queue.
在将来的某个时刻,回调会触发,并且该调用将从该队列中删除。
In the meantime, you can still process stuff.同时,您仍然可以处理内容。
... or memory leaks?
……还是内存泄漏?
The setTimeout
callback is within a closure. setTimeout
回调位于闭包内。 As soon as setTimeout
invokes the callback, it becomes eligible for garbage collection.一旦
setTimeout
调用回调,它就可以进行垃圾回收。
Unless you get many millions of bookings within the 900000ms timeframe, you have nothing to worry about;除非您在 900000 毫秒的时间范围内获得数百万次预订,否则您无需担心; the number of course depends on the memory size you allocated to your Node.js application.
当然,数量取决于您分配给 Node.js 应用程序的内存大小。
Of course if you do get that many requests-per-second, you have other, more important stuff to worry about.当然,如果您每秒收到那么多请求,那么您还有其他更重要的事情需要担心。
It won't have performance issues or memory leak problems, but using a 15 minutes timeout function might be problematic for debugging and maintaining.它不会有性能问题或内存泄漏问题,但使用 15 分钟超时功能可能会导致调试和维护问题。
Especially something like cancelling a booking should be solved another way.特别是取消预订之类的事情应该以另一种方式解决。
You always should write your node application in a way that:您始终应该以以下方式编写您的节点应用程序:
So assuming that you already use a database for the booking process, then you should also do the 15 minute timing within the database.因此,假设您已经在预订过程中使用了数据库,那么您还应该在数据库中进行 15 分钟计时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.