📜  Node.js 代理.maxFreeSockets 方法(1)

📅  最后修改于: 2023-12-03 14:44:42.330000             🧑  作者: Mango

Node.js proxy.maxFreeSockets 方法介绍

简介

在 Node.js 中, http, https 模块提供了创建 HTTP/HTTPS 服务器和客户端的 API, 包括请求和响应等对象。http.Agent 和 https.Agent类似一个连接池,内部维护了若干个连接对象,当发起请求时,可以从Agent对象内的连接池中取出一个可用的连接对象,发起请求,完成之后将连接对象放回连接池中,供后续请求复用。由于连接池有其自身的限制,如果开发者想要控制连接请求的数量,则需要使用到 maxFreeSockets 方法。

方法介绍

proxy.maxFreeSockets 方法用于设置代理服务器连接池中,最多可保留的 free sockets 的数量。默认情况下, http.Agent.defaultMaxFreeSocketshttps.Agent.defaultMaxFreeSockets 分别为 256

在 Node.js 中, http.globalAgenthttps.globalAgent 是默认的全局代理。我们可以通过以下方法来设置 http.globalAgent.maxFreeSocketshttps.globalAgent.maxFreeSockets 的值:

http.globalAgent.maxFreeSockets = 100
https.globalAgent.maxFreeSockets = 100

如果我们想要设置某一个代理的最多可保留的 free sockets 的数量,可以通过以下方法创建一个代理并设置其最多可保留的 free sockets 的数量:

const HttpsProxyAgent = require('https-proxy-agent')

const proxy = new HttpsProxyAgent('https://127.0.0.1:8080')
proxy.maxFreeSockets = 100
示例

以下示例代码创建了一个全局 https 代理,然后分别使用代理和不使用代理,向同一个服务器发起100个请求,并输出每个 Agent 监听的事件:

const https = require('https')
const HttpsProxyAgent = require('https-proxy-agent')

// 创建代理
const proxy = new HttpsProxyAgent('https://127.0.0.1:8080')
proxy.maxFreeSockets = 100

// 未使用代理的请求
const noProxyAgent = new https.Agent({
  keepAlive: true,
  maxFreeSockets: 100,
  keepAliveMsecs: 10000,
})

// 使用代理的请求
const proxyAgent = new https.Agent({
  keepAlive: true,
  maxFreeSockets: 100,
  keepAliveMsecs: 10000,
  proxy,
})

const url = 'https://www.example.com'

// 使用 noProxyAgent 发起请求
for(let i=0;i<100;i++){
  https.get(url, {
    agent: noProxyAgent,
  }, (res)=>{
    console.log('noProxy: status: ', res.statusCode, 'conn: ', noProxyAgent.getCurrentStatus())
    res.on('end', ()=>{
      console.log(`noProxy: requests#${i + 1} end`)
    })
  }).on('error', (err)=>{
    console.error(`noProxy: requests#${i + 1} error: `, err.message)
  })
}

// 使用 proxyAgent 发起请求
for(let i=0;i<100;i++){
  https.get(url, {
    agent: proxyAgent,
  }, (res)=>{
    console.log('proxy: status: ', res.statusCode, 'conn: ', proxyAgent.getCurrentStatus())
    res.on('end', ()=>{
      console.log(`proxy: requests#${i + 1} end`)
    })
  }).on('error', (err)=>{
    console.error(`proxy: requests#${i + 1} error: `, err.message)
  })
}

以上代码输出了每个请求完成时各个 Agent 监听的事件:

proxy: status:  200 conn:  { socket: 100, free: 0, queue: 0 }
noProxy: status:  200 conn:  { socket: 100, free: 99, queue: 0 }
noProxy: requests#100 end
noProxy: status:  200 conn:  { socket: 100, free: 99, queue: 0 }
noProxy: requests#99 end
proxy: status:  200 conn:  { socket: 100, free: 0, queue: 0 }
noProxy: status:  200 conn:  { socket: 100, free: 99, queue: 0 }
noProxy: requests#98 end
proxy: status:  200 conn:  { socket: 100, free: 0, queue: 0 }
noProxy: status:  200 conn:  { socket: 100, free: 99, queue: 0 }
noProxy: requests#97 end
noProxy: status:  200 conn:  { socket: 100, free: 99, queue: 0 }
noProxy: requests#96 end
proxy: status:  200 conn:  { socket: 100, free: 0, queue: 0 }
proxy: requests#100 end
proxy: status:  200 conn:  { socket: 100, free: 0, queue: 0 }
proxy: requests#99 end
proxy: status:  200 conn:  { socket: 100, free: 0, queue: 0 }
proxy: requests#98 end
proxy: status:  200 conn:  { socket: 100, free: 0, queue: 0 }
proxy: requests#97 end
proxy: status:  200 conn:  { socket: 100, free: 0, queue: 0 }
proxy: requests#96 end

从结果中可以看到,使用代理的请求在连接数达到限制时,阻塞等待直到连接数释放。而未使用代理的请求在连接数满载时,不会阻塞等待,而是继续发起新的连接请求,当连接数超过限制时,连接对象会被销毁,释放给连接池供复用。

总结

proxy.maxFreeSockets 方法是控制代理服务器连接池中 free sockets 数量的方法,为了保证应用程序能够正常的工作,我们需要合理地设置 maxFreeSockets 的值。在实际的开发过程中,通过以上介绍和示例,我们应该更好地使用代理服务器和连接池,提高应用程序的性能。