我正在学习stream-adventure课程。其中一项任务是创建一个 http 服务器,将所有请求转换为大写并在响应中返回。
现在我设法让它工作并且任务通过了。但是,控制台给了我一个 TimeoutOverflowWarning。
(node:15710) TimeoutOverflowWarning: 4294967296000 does not fit into a 32-bit signed integer.
Timer duration was truncated to 2147483647.
(node:15710) TimeoutOverflowWarning: 4294967296000 does not fit into a 32-bit signed integer.
Timer duration was truncated to 2147483647.
Run Code Online (Sandbox Code Playgroud)
我想知道这是内存泄漏还是由我的代码引起的,或者是否是其他原因。因为错误消息中提到了 32 位,我想知道这是否与我使用 2016 年的 Macbook Pro 以 64 位运行有关。(节点 v10.17.0)
代码:
'use-strict'
const through = require('through2')
const http = require('http')
const port = process.argv[2]
const uppercaser = through(function (buffer, _, next) {
this.push(buffer.toString().toUpperCase())
next()
});
const server = http.createServer(function (req, res) {
if (req.method === 'POST') {
res.writeHead(200, { 'Content-Type': 'text/plain' })
req.pipe(uppercaser).pipe(res)
} else {
res.writeHead(404)
res.end()
}
});
server.listen(port)
Run Code Online (Sandbox Code Playgroud)
当我发现这种类型的错误是在我使用函数时setInterval。
这是因为超时的上限是 2147483647,这是 32 位 int 的最大限制。
如果你愿意,你可以制作自己的setInterval包装纸
function setDaysTimeout(callback,days) {
// 86400 seconds in a day
let msInDay = 86400*1000;
let dayCount = 0;
let timer = setInterval(function() {
dayCount++; // a day has passed
if (dayCount === days) {
clearInterval(timer);
callback.apply(this, []);
}
}, msInDay);
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
setDaysTimeout(function() {
console.log('Four days gone');
}, 4); // fire after 4 days
Run Code Online (Sandbox Code Playgroud)
setTimeout这是与\函数相关的问题setInterval。如您所见,它们的限制为 32 位。因此,它node警告您:
> setTimeout(console.log, +Infinity)
> (node:19903) TimeoutOverflowWarning: Infinity does not fit into a 32-bit signed integer.
Timeout duration was set to 1.
Run Code Online (Sandbox Code Playgroud)
由于您的代码没有任何这些,因此库中的某些代码似乎有问题。
我建议使用 flag 运行node来--trace-warnings 查找警告来源:
--跟踪警告
打印进程警告的堆栈跟踪(包括弃用)。
| 归档时间: |
|
| 查看次数: |
16263 次 |
| 最近记录: |