出现 Axios 错误:拨打大量电话时连接 ETIMEDOUT

2024-03-10

从我的 Azure Function 进行大量调用时出现此错误。这个错误是什么意思?如何排除故障?我猜我的 TCP 套接字用完了?我真的不知道如何在功能应用程序菜单中检查它。不过,我检查了 Azure Maps API 的日志,没有错误或掉线的记录,所以我认为这绝对是 axios/function 问题。

我看到了一些添加建议Connection:Keep-Aliveheader 甚至创建 axiosinstance并重复使用它。但是我不确定我的问题是否与此有关。

Error: connect ETIMEDOUT 13.107.42.21:443
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {   errno: 'ETIMEDOUT',   code: 'ETIMEDOUT',   syscall: 'connect',   address: '13.107.42.21',   port: 443,   config: {
    url: 'https://atlas.microsoft.com/search/fuzzy/json?api-version=1.0&subscription-key= myKey &query=myAddress USA',
    method: 'get',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'x-ms-client-id': 'my client ID',
      'User-Agent': 'axios/0.19.2'
    },
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    adapter: [Function: httpAdapter],
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    validateStatus: [Function: validateStatus],
    host: 'atlas.microsoft.com',
    data: undefined   },   request: Writable {
    _writableState: WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: false,
      needDrain: false,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: true,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: true,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      afterWriteTickInfo: null,
      bufferedRequest: null,
      lastBufferedRequest: null,
      pendingcb: 0,
      prefinished: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: false,
      bufferedRequestCount: 0,
      corkedRequestsFree: [Object]
    },
    writable: true,
    _events: [Object: null prototype] {
      response: [Function: handleResponse],
      error: [Function: handleRequestError]
    },
    _eventsCount: 2,
    _maxListeners: undefined,
    _options: {
      protocol: 'https:',
      maxRedirects: 21,
      maxBodyLength: 10485760,
      path: '/search/fuzzy/json?api-version=1.0&subscription-key= myKey &query=myAddress%20USA',
      method: 'GET',
      headers: [Object],
      agent: undefined,
      agents: [Object],
      auth: undefined,
      hostname: 'atlas.microsoft.com',
      port: null,
      nativeProtocols: [Object],
      pathname: '/search/fuzzy/json',
      search: '?api-version=1.0&subscription-key= myKey &query=myAddress%20USA'
    },
    _redirectCount: 0,
    _redirects: [],
    _requestBodyLength: 0,
    _requestBodyBuffers: [],
    _onNativeResponse: [Function],
    _currentRequest: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [TLSSocket],
      connection: [TLSSocket],
      _header: 'GET /search/fuzzy/json?api-version=1.0&subscription-key= myKey &query=myAddress%20USA HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'x-ms-client-id: my client ID\r\n' +
        'User-Agent: axios/0.19.2\r\n' +
        'Host: atlas.microsoft.com\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      insecureHTTPParser: undefined,
      path: '/search/fuzzy/json?api-version=1.0&subscription-key= myKey &query=myAddress%20USA',
      _ended: false,
      res: null,
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      _redirectable: [Circular],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    _currentUrl: 'https://atlas.microsoft.com/search/fuzzy/json?api-version=1.0&subscription-key= myKey &query=myAddress%20USA',
    [Symbol(kCapture)]: false   },   response: undefined,   isAxiosError: true,   toJSON: [Function] }

因此,在与支持人员交谈了整整一个月之后,他们告诉我我遇到了 SNAT 问题。基本上用完了出站端口...这对我来说没有意义,因为我使用的是单个 axios 实例并全面共享它。然而,在阅读了提供的文档后,我得出的结论是需要进行一些额外的更改。

所以我的主文件夹(在 wwwroot 文件夹中)中有一个名为 getAxios.js 的文件,该文件将创建到基本 url 的连接。

目录布局:

-wwwroot

--getAxios.js

--myFunction

getAxios.js代码:

const axios = require('axios')
const https = require('https')
const domain = 'https://atlas.microsoft.com'
let instance

module.exports = function (context)
{
    if (!instance)
    {
        //create axios instance
        instance = axios.create({
            baseURL: domain,
            timeout: 60000, //optional
            httpsAgent: new https.Agent({ keepAlive: true }),
            headers: {'Content-Type':'application/xml'}
        })
    }

    return instance;
}

然而我没有timeout之前设置为任何特定的内容,但我没有指定keepAlive:true。添加这两个附加参数解决了我的问题。

为了更多地解释一下端口被耗尽的情况,当发出请求并且 keepAlive 为 false 时,端口连接将关闭,但在可以再次使用之前它有一个小的超时持续时间。当您处理大量请求时,许多端口处于这种状态并且无法使用,最终会遇到此问题。这是 MS 支持人员向我解释的。我确信我可能在网络方面用词错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

出现 Axios 错误:拨打大量电话时连接 ETIMEDOUT 的相关文章

随机推荐