【Hacker News搬运】setBigTimeout
-
Title: setBigTimeout
setBigTimeout
Text:
Url: https://evanhahn.com/set-big-timeout/
很抱歉,我无法直接访问或抓取外部网站的内容。不过,我可以根据你提供的链接描述来给出一些指导。 Evan Hahn 的网站可能是一篇关于如何设置超时(timeout)的文章。以下是对这个主题的一般性描述和总结: --- 标题:设置大超时(Set Big Timeout) 总结: 1. **背景介绍**:在编程和网页开发中,超时(timeout)是一个非常重要的概念,它用于限制一个操作执行的时间。如果操作在指定的时间内没有完成,就会触发超时。 2. **为什么要设置超时**:设置超时可以避免程序无限期地等待某个可能永远不会完成的操作,这样可以提高程序的健壮性和响应速度。 3. **如何设置超时**: - **在Web开发中**:可以使用JavaScript中的`setTimeout`函数来设置超时。 - **在服务器端编程中**:不同的编程语言和框架有不同的方式来设置超时,例如在Node.js中可以使用`http.createServer()`的`timeout`选项。 4. **设置大超时的注意事项**: - **不要设置过长的超时时间**:虽然大超时可以避免某些操作失败,但如果设置得太长,可能会导致用户体验变差,比如页面加载时间过长。 - **监控和调试**:在设置大超时后,应该监控程序的执行情况,确保没有发生意外的长时间运行。 5. **示例代码**:文章可能会提供一些设置超时的示例代码,展示如何在不同的编程环境中实现。 6. **总结**:合理设置超时是确保程序高效运行的关键,但需要平衡超时时间与用户体验。 --- 请注意,以上内容是基于一般性描述的总结,并不是针对Evan Hahn网站上的具体内容。如果你需要了解该网站的具体内容,建议直接访问该网站并阅读文章。
Post by: cfj
Comments:
n2d4: The default behaviour of setTimeout seems problematic. Could be used for an exploit, because code like this might not work as expected:<p><pre><code> const attackerControlled = ...;
if (attackerControlled < 60_000) {
throw new Error("Must wait at least 1min!");
}setTimeout(() => { console.log("Surely at least 1min has passed!"); }, attackerControlled);
</code></pre>
The attacker could set the value to a comically large number and the callback would execute immediately. This also seems to be true for NaN. The better solution (imo) would be to throw an error, but I assume we can't due to backwards compatibility.n2d4: setTimeout的默认行为似乎有问题。可能被用于漏洞利用,因为这样的代码可能无法按预期工作:<p><pre><code>const attackerControlled=。。。;如果(攻击者控制<;60_000){抛出新错误(“必须等待至少1分钟!”);}setTimeout(()=>;{console.log(“肯定已经过去了至少1分钟!”);},攻击者受控);</code></pre>攻击者可以将该值设置为一个滑稽的大数字,回调将立即执行。NaN似乎也是如此。更好的解决方案(imo)是抛出错误,但我认为我们可以;t由于向后兼容性。
keepamovin: This is excellent. But I was hoping for a setTimeout that survived JavaScript environment restarts. Maybe <i>setBigReliableTimeout</i> is in your future? Hahaha!
keepamovin: 这太棒了。但我希望setTimeout能在JavaScript环境重启后幸存下来。也许<i>setBigReliableTimeout</i>会出现在你的未来?哈哈哈!:)
jackconsidine: This type of thing is actually practical. Google Cloud Tasks have a max schedule date of 30 days in the future so the typical workaround is to chain tasks. As other commenters have suggested you can also set a cron check. This has more persistent implications on your database, but chaining tasks can fail in other ways, or explode if there are retries and a failed request does trigger a reschedule (I hate to say I’m speaking from experience)
jackconsidine: 这种东西其实很实用。Google Cloud Tasks的最长计划日期为未来30天,因此典型的解决方法是链接任务。正如其他评论者所建议的那样,你也可以设置一个cron检查。这对您的数据库有更持久的影响,但链接任务可能会以其他方式失败,或者在重试时爆发,失败的请求确实会触发重新安排(我不想说我是凭经验说的)
yifanl: If we're pedantic, this doesn't actually do what's advertised, this would be waiting X timeouts worth of event cycles rather than just the one for a true Big timeout, assuming the precision matters when you're stalling a function for 40 days.
yifanl: 如果我们;这太迂腐了,这不是;实际上不做什么;正如广告所说,这将等待相当于X个超时的事件周期,而不仅仅是真正的大超时,假设精度在您需要时很重要;将一个函数重新暂停40天。
yesco: > In most JavaScript runtimes, this duration is represented as a 32-bit signed integer<p>I thought all numbers in JavaScript were basically some variation of double precision floating points, if so, why is setTimeout limited to a smaller 32bit signed integer?<p>If this is true, then if I pass something like "0.5", does it round the number when casting it to an integer? Or does it execute the callback after half a millisecond like you would expect it would?
yesco: >;在大多数JavaScript运行时中,这个持续时间表示为一个32位有符号整数<p>我认为JavaScript中的所有数字基本上都是双精度浮点的变体,如果是这样,为什么setTimeout被限制为一个较小的32位有义整数<p> 如果这是真的,那么如果我通过类似";0.5英寸;,在将数字转换为整数时,它是否对数字进行四舍五入?或者,它是否像您预期的那样在半毫秒后执行回调?