Egg: curl请求后端接口 timeout 超时时间设置问题。

Created on 30 Oct 2018  ·  12Comments  ·  Source: eggjs/egg

先贴出code 片段,

const resData = await service.cloud.common.sendApi(
        'simulator',
        'detail',
        query,
        {
          timeout: 20000,
        },
    );
async sendApi(group, name, data, options?) {
    const {ctx} = this;
    const url = this.connectionUrl(group, name);
    return ctx.helper.curl(url, {data, ...options});
  }



md5-b7454204229b3b6a1509154dfe231571



export async function curl(this: Context, url: string, opt?: RequestOptions): Promise<any> {
  const option = {
    beforeRequest: (options) => {
      options.headers['x-requestid'] = this.ctx.get('x-requestid');
    },
    contentType: 'application/x-www-form-urlencoded',
    method: 'post',
    dataType: 'json',
    timeout: 3000,
    headers: {
      'x-requestid': this.ctx.get('x-requestid'), // 全局请求 id,方便日志跟踪
    },
    ...opt,
  };



md5-080a4426a63278f200065c3c0af2d20d



2018-10-30 20:37:56,585 INFO 93922 [-/127.0.0.1/-/20ms POST /cloud/simulation/saveConfig] 请求URL=> http://0.0.0.0:7001/image/detail
2018-10-30 20:37:56,585 INFO 93922 [-/127.0.0.1/-/20ms POST /cloud/simulation/saveConfig] 请求参数=> {"contentType":"application/x-www-form-urlencoded","method":"post","dataType":"json","timeout":20000,"headers":{"x-requestid":""},"data":{"warehouseID":"219676137494675475","frameConfig":"[{\"initAreaID\":\"257\",\"frameCount\":16},{\"initAreaID\":\"271\",\"frameCount\":16},{\"initAreaID\":\"272\",\"frameCount\":16},{\"initAreaID\":\"273\",\"frameCount\":16}]","robotConfig":"[{\"initAreaID\":\"296\",\"stationID\":\"296\",\"robotCount\":3},{\"initAreaID\":\"619\",\"stationID\":\"619\",\"robotCount\":3},{\"initAreaID\":\"621\",\"stationID\":\"621\",\"robotCount\":3},{\"initAreaID\":\"623\",\"stationID\":\"623\",\"robotCount\":4},{\"initAreaID\":\"318\",\"stationID\":\"318\",\"robotCount\":3},{\"initAreaID\":\"316\",\"stationID\":\"316\",\"robotCount\":4}]","workSpeed":"[{\"parentID\":\"220267811249848340\",\"processID\":\"220267811249913876\",\"stationList\":[{\"stationID\":\"316\",\"speed\":900}]},{\"parentID\":\"220267811249848340\",\"processID\":\"220267811249979412\",\"stationList\":[{\"stationID\":\"296\",\"speed\":900}]},{\"parentID\":\"220267811250044948\",\"processID\":\"220267811250110484\",\"stationList\":[{\"stationID\":\"318\",\"speed\":900}]},{\"parentID\":\"220267811250044948\",\"processID\":\"220267811266625556\",\"stationList\":[{\"stationID\":\"621\",\"speed\":900}]},{\"parentID\":\"220267811266691092\",\"processID\":\"220267811266756628\",\"stationList\":[{\"stationID\":\"316\",\"speed\":900}]},{\"parentID\":\"220267811266691092\",\"processID\":\"220267811266822164\",\"stationList\":[{\"stationID\":\"619\",\"speed\":900}]},{\"parentID\":\"220267811266887700\",\"processID\":\"220267811266953236\",\"stationList\":[{\"stationID\":\"318\",\"speed\":900}]},{\"parentID\":\"220267811266887700\",\"processID\":\"220267811267018772\",\"stationList\":[{\"stationID\":\"623\",\"speed\":900}]}]","produceChance":"[{\"stationID\":\"296\",\"processList\":[{\"processID\":\"220267811249979412\",\"chance\":100}]},{\"stationID\":\"619\",\"processList\":[{\"processID\":\"220267811266822164\",\"chance\":100}]},{\"stationID\":\"621\",\"processList\":[{\"processID\":\"220267811266625556\",\"chance\":100}]},{\"stationID\":\"623\",\"processList\":[{\"processID\":\"220267811267018772\",\"chance\":100}]},{\"stationID\":\"318\",\"processList\":[{\"processID\":\"220267811250110484\",\"chance\":40},{\"processID\":\"220267811266953236\",\"chance\":60}]},{\"stationID\":\"316\",\"processList\":[{\"processID\":\"220267811249913876\",\"chance\":40},{\"processID\":\"220267811266756628\",\"chance\":60}]}]","simSpeed":2,"workTerm":60}}




2018-10-30 20:38:16,603 INFO 93922 [-/127.0.0.1/-/20038ms POST /cloud/simulation/saveConfig] ****代码报错咯****
2018-10-30 20:38:16,603 INFO 93922 [-/127.0.0.1/-/20038ms POST /cloud/simulation/saveConfig] 错误信息=> ResponseTimeoutError: Response timeout for 20000ms, POST http://0.0.0.0:7001/image/detail -1 (connected: true, keepalive socket: false, agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":1,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"sockets":{},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 0)
headers: {}
2018-10-30 20:38:16,607 ERROR 93922 nodejs.ResponseTimeoutError: Response timeout for 20000ms, POST http://0.0.0.0:7001/image/detail -1 (connected: true, keepalive socket: false, agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":1,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"sockets":{},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 0)
headers: {}
headers: {}
    at Timeout._onTimeout (/Users/zhaobin/Workspace/ui-hetu/node_modules/urllib/lib/urllib.js:832:15)
    at ontimeout (timers.js:427:11)
    at tryOnTimeout (timers.js:289:5)
    at listOnTimeout (timers.js:252:5)
    at Timer.processTimers (timers.js:212:10)
name: "ResponseTimeoutError"
requestId: 1



md5-89bf5ec19ff41e6150825eb6f1a5231e



2018-10-30 20:55:46,178 INFO 94198 [-/127.0.0.1/-/20ms POST /cloud/simulation/saveConfig] 请求URL=> http://0.0.0.0:7001/image/detail
2018-10-30 20:55:46,178 INFO 94198 [-/127.0.0.1/-/20ms POST /cloud/simulation/saveConfig] 请求参数=> {"contentType":"application/x-www-form-urlencoded","method":"post","dataType":"json","timeout":40000,"headers":{"x-requestid":""},"data":{"warehouseID":"219676137494675475","frameConfig":"[{\"initAreaID\":\"257\",\"frameCount\":16},{\"initAreaID\":\"271\",\"frameCount\":16},{\"initAreaID\":\"272\",\"frameCount\":16},{\"initAreaID\":\"273\",\"frameCount\":16}]","robotConfig":"[{\"initAreaID\":\"296\",\"stationID\":\"296\",\"robotCount\":3},{\"initAreaID\":\"619\",\"stationID\":\"619\",\"robotCount\":3},{\"initAreaID\":\"621\",\"stationID\":\"621\",\"robotCount\":3},{\"initAreaID\":\"623\",\"stationID\":\"623\",\"robotCount\":4},{\"initAreaID\":\"318\",\"stationID\":\"318\",\"robotCount\":3},{\"initAreaID\":\"316\",\"stationID\":\"316\",\"robotCount\":4}]","workSpeed":"[{\"parentID\":\"220267811249848340\",\"processID\":\"220267811249913876\",\"stationList\":[{\"stationID\":\"316\",\"speed\":900}]},{\"parentID\":\"220267811249848340\",\"processID\":\"220267811249979412\",\"stationList\":[{\"stationID\":\"296\",\"speed\":900}]},{\"parentID\":\"220267811250044948\",\"processID\":\"220267811250110484\",\"stationList\":[{\"stationID\":\"318\",\"speed\":900}]},{\"parentID\":\"220267811250044948\",\"processID\":\"220267811266625556\",\"stationList\":[{\"stationID\":\"621\",\"speed\":900}]},{\"parentID\":\"220267811266691092\",\"processID\":\"220267811266756628\",\"stationList\":[{\"stationID\":\"316\",\"speed\":900}]},{\"parentID\":\"220267811266691092\",\"processID\":\"220267811266822164\",\"stationList\":[{\"stationID\":\"619\",\"speed\":900}]},{\"parentID\":\"220267811266887700\",\"processID\":\"220267811266953236\",\"stationList\":[{\"stationID\":\"318\",\"speed\":900}]},{\"parentID\":\"220267811266887700\",\"processID\":\"220267811267018772\",\"stationList\":[{\"stationID\":\"623\",\"speed\":900}]}]","produceChance":"[{\"stationID\":\"296\",\"processList\":[{\"processID\":\"220267811249979412\",\"chance\":100}]},{\"stationID\":\"619\",\"processList\":[{\"processID\":\"220267811266822164\",\"chance\":100}]},{\"stationID\":\"621\",\"processList\":[{\"processID\":\"220267811266625556\",\"chance\":100}]},{\"stationID\":\"623\",\"processList\":[{\"processID\":\"220267811267018772\",\"chance\":100}]},{\"stationID\":\"318\",\"processList\":[{\"processID\":\"220267811250110484\",\"chance\":40},{\"processID\":\"220267811266953236\",\"chance\":60}]},{\"stationID\":\"316\",\"processList\":[{\"processID\":\"220267811249913876\",\"chance\":40},{\"processID\":\"220267811266756628\",\"chance\":60}]}]","simSpeed":2,"workTerm":60}}
2018-10-30 20:56:16,203 INFO 94198 [-/127.0.0.1/-/30045ms POST /cloud/simulation/saveConfig] ****代码报错咯****
2018-10-30 20:56:16,203 INFO 94198 [-/127.0.0.1/-/30045ms POST /cloud/simulation/saveConfig] 错误信息=> ResponseError: socket hang up, POST http://0.0.0.0:7001/image/detail -1 (connected: true, keepalive socket: false, agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":1,"errorSocketCount":0,"timeoutSocketCount":1,"requestCount":0,"freeSockets":{},"sockets":{},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 0)
headers: {}
2018-10-30 20:56:16,206 ERROR 94198 nodejs.ResponseError: socket hang up, POST http://0.0.0.0:7001/image/detail -1 (connected: true, keepalive socket: false, agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":1,"errorSocketCount":0,"timeoutSocketCount":1,"requestCount":0,"freeSockets":{},"sockets":{},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 0)
headers: {}
headers: {}
    at createHangUpError (_http_client.js:312:15)
    at Socket.socketCloseListener (_http_client.js:344:23)
    at Socket.emit (events.js:187:15)
    at TCP._handle.close [as _onclose] (net.js:598:12)
code: "ECONNRESET"
name: "ResponseError"
data: undefined
path: "/image/detail"
status: -1
headers: {}
res: {"status":-1,"statusCode":-1,"headers":{},"size":0,"aborted":false,"rt":30007,"keepAliveSocket":false,"requestUrls":["http://0.0.0.0:7001/image/detail"],"timing":null,"remoteAddress":"127.0.0.1","remotePort":7001,"socketHandledRequests":1,"socketHandledResponses":0}
pid: 94198
hostname: localhost
  • Node Version: 8.9.0
  • Egg Version: 2.2.1

Most helpful comment

问题查到了!这个设置全局socket时间,之前直接export的!感谢你咯!

config.httpclient = {
    httpAgent: {
      timeout: 63000,
    },
  };

All 12 comments

@fengmk2 I'm sorry to trouble you, can you help me to solve the problem please . Thank you very much.

看下 config.httpclient 配置,这个是 keepalive 的 socket 超时了

exports.httpclient = {
  // 是否开启本地 DNS 缓存,默认关闭,开启后有两个特性
  // 1. 所有的 DNS 查询都会默认优先使用缓存的,即使 DNS 查询错误也不影响应用
  // 2. 对同一个域名,在 dnsCacheLookupInterval 的间隔内(默认 10s)只会查询一次
  enableDNSCache: false,
  // 对同一个域名进行 DNS 查询的最小间隔时间
  dnsCacheLookupInterval: 10000,
  // DNS 同时缓存的最大域名数量,默认 1000
  dnsCacheMaxLength: 1000,

  request: {
    // 默认 request 超时时间
    timeout: 3000,
  },

  httpAgent: {
    // 默认开启 http KeepAlive 功能
    keepAlive: true,
    // 空闲的 KeepAlive socket 最长可以存活 4 秒
    freeSocketKeepAliveTimeout: 4000,
    // 当 socket 超过 30 秒都没有任何活动,就会被当作超时处理掉
    timeout: 30000,
    // 允许创建的最大 socket 数
    maxSockets: Number.MAX_SAFE_INTEGER,
    // 最大空闲 socket 数
    maxFreeSockets: 256,
  },

  httpsAgent: {
    // 默认开启 https KeepAlive 功能
    keepAlive: true,
    // 空闲的 KeepAlive socket 最长可以存活 4 秒
    freeSocketKeepAliveTimeout: 4000,
    // 当 socket 超过 30 秒都没有任何活动,就会被当作超时处理掉
    timeout: 30000,
    // 允许创建的最大 socket 数
    maxSockets: Number.MAX_SAFE_INTEGER,
    // 最大空闲 socket 数
    maxFreeSockets: 256,
  },
};

您是说要设置这些么?我目前httpclient没有设置,中间我设置过,但是没有什么用!所以很疑惑~

2018-10-30 20:56:16,206 ERROR 94198 nodejs.ResponseError: socket hang up, POST http://0.0.0.0:7001/image/detail -1 (connected: true, keepalive socket: false, agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":1,"errorSocketCount":0,"timeoutSocketCount":1,"requestCount":0,"freeSockets":{},"sockets":{},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 0)

可以看出你中间的网络层在30s的是就断开了这个无响应的 socket 。可以看看中间的 slb,nginx 。

httpclient 的timeout 是客户端 timeout,不代表服务端,服务端 timeout 是自定义的,无法控制。

感谢您的提问,可是我一开始也是这么觉得,但是问了nginx层,他们说没有设置,然后我本地用egg又起了一个简单的默认服务,中间定时40秒给出响应,还能复现上述问题。而且我也不知道在后端设置响应超时,感觉后端不用设置吧!可能nginx可以设置,但是我本地的服务,没有走nginx这层呀!这么晚打扰你了,抱歉哈!

你将agent的timeout设置成60s看看?

问题查到了!这个设置全局socket时间,之前直接export的!感谢你咯!

config.httpclient = {
    httpAgent: {
      timeout: 63000,
    },
  };

@xiaodiao1212 也感谢你的反馈,我感觉这里应该有 bug,request timeout 大于 agent timeout 的时候出问题了。

@fengmk2 我也遇到了类似问题,想问下curl的timeout和httpclient request timeout和agent timeout的优先级关系是?

@14glwu 如果 request timeout > agent timeout,那么会以 request timeout 为准 https://github.com/node-modules/urllib/pull/300/files ,agent 用完会恢复到 agent timeout。

请问如果大文件上传超时了,在哪设置呢?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

killagu picture killagu  ·  48Comments

XadillaX picture XadillaX  ·  44Comments

occultskyrong picture occultskyrong  ·  35Comments

fengmk2 picture fengmk2  ·  51Comments

popomore picture popomore  ·  53Comments