惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Engineering at Meta
Engineering at Meta
量子位
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
月光博客
月光博客
A
About on SuperTechFans
博客园 - 聂微东
Spread Privacy
Spread Privacy
B
Blog
NISL@THU
NISL@THU
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
T
Threatpost
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
P
Privacy & Cybersecurity Law Blog
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
C
Check Point Blog
O
OpenAI News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Know Your Adversary
Know Your Adversary
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
P
Privacy International News Feed
B
Blog RSS Feed
Google DeepMind News
Google DeepMind News
H
Heimdal Security Blog
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
V
Vulnerabilities – Threatpost
博客园 - 司徒正美
C
CERT Recently Published Vulnerability Notes
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives

博客园 - MK2

cnpmcore 超大 JSON parse 性能优化 基于 Postgres 实现一个 Job Queue Graceful exit with cluster and pm Use Blanket.js instead of jscover 使用 connect-domain 捕获异步调用中出现的异常 Defense hash algorithm collision 防御hash算法冲突导致拒绝服务器 fibonacci(40) benchmark [nodejs]保证你的程序死了还能复活:forever and forever webui [nodejs]Buffer vs String Nodejs "Hello world" benchmark npm 资源库镜像 NodeBlog v0.2.0 更新说明 关于__proto__的链式记忆 NAE支持自定义域名了 Nodejs 离线文档下载 nodejs读写大文件 在jQuery 1.5+ 的jqXHR上监听文件上传进度(xhr.upload) 关于Nodejs中Buffer释放的二三事 nodejs web开发入门: Simple-TODO Nodejs 实现版
Nodejs HTTP请求的超时处理(Nodejs HTTP Client Request Timeout Handle)
MK2 · 2011-08-09 · via 博客园 - MK2

问题

Nodejs原生的http.request 方法是不支持设置超时参数的,
而网络请求经常会遇到超时的情况,特别是对于外部网络,如果不处理超时,发起的请求将会一直卡主,消耗的系统资源也不能及时被释放。

解决方案(旧)

定时器:通过定时器,当timeout事件触发的时候,主动调用req.abort() 终止请求,
然后返回超时异常。

Request Timeout & Response Timeout

  • 超时有请求超时(Request Timeout):HTTP客户端发起请求到接受到HTTP服务器端返回响应头的这段时间, 如果超出设定时间,则表示请求超时。
  • 响应超时(Response Timeout):HTTP服务器端开始发送响应数据到HTTP客户端接收全部数据的这段时间, 如果超出设定时间,则表示响应超时。

示例代码:Timeout Demo

var http = require('http');

var request_timer = null, req = null;
// 请求5秒超时
request_timer = setTimeout(function() {
    req.abort();
    console.log('Request Timeout.');
}, 5000);

var options = {
    host: 'www.google.com',
    port: 80,
    path: '/'
};
req = http.get(options, function(res) {
    clearTimeout(request_timer);

    // 等待响应60秒超时
    var response_timer = setTimeout(function() {
        res.destroy();
        console.log('Response Timeout.');
    }, 60000);

    console.log("Got response: " + res.statusCode);
    var chunks = [], length = 0;
    res.on('data', function(chunk) {
        length += chunk.length;
        chunks.push(chunk);
    });
    res.on('end', function() {
        clearTimeout(response_timer);
        var data = new Buffer(length);
        // 延后copy
        for(var i=0, pos=0, size=chunks.length; i

有爱

^_^ 希望本文对你有用。