wechat-api icon indicating copy to clipboard operation
wechat-api copied to clipboard

请求可以开启 keepAlive 吗?

Open shaman2009 opened this issue 7 years ago • 4 comments

最近在用本库的时候遇到一个问题,并发推送 15000+ 条客服消息的时候会报错, 解决方法暂时把 timeout 设置的很大, 想咨询下各位, keepAlive 默认好像是不开启的?是可以手动开启吗?文档中没找到相关描述。

shaman2009 avatar Apr 12 '17 02:04 shaman2009

urllib 是默认设置了 keepAlive 的。

JacksonTian avatar Apr 12 '17 02:04 JacksonTian

默认是开启了Agent,设置了最大sockets连接数

exports.httpsAgent = new https.Agent();
exports.httpsAgent.maxSockets = 1000;

但是在Agent的默认配置里:

image

所以实际是没有开启keepAlive的吗?我理解的是否有问题?

实际运行的时候,可以看到错误日志里显示的keepAliveSocket也是false, 说明在req.once('socket')的时候也是没有reuse的,具体可以参见这个issue

yey avatar Apr 12 '17 03:04 yey

this works well

const http = require('http')
const https = require('http')
const WechatAPI = require('wechat-api')

let api = new WechatAPI(WECHAT.appId, WECHAT.appsecret)
let maxSockets = 100
api.setOpts({
	timeout: 150000,
	httpAgent: new http.Agent({
		keepAlive: true,
		maxSockets
	}),
	httpsAgent: new https.Agent({
		keepAlive: true,
		maxSockets
	})
})

yey avatar Apr 12 '17 05:04 yey

@JacksonTian 虽然issue close 了 但是现在看下来默认情况下 keep-alive 是关闭的咯? 可能需要一个 patch ?

shaman2009 avatar Apr 12 '17 07:04 shaman2009