当前位置:网站首页>叁拾壹- NodeJS简单代理池(合) 之 MongoDB 链接数爆炸了
叁拾壹- NodeJS简单代理池(合) 之 MongoDB 链接数爆炸了
2022-06-09 09:59:00 【BerryBC】
1。前文再续
惊现 MongoDB 链接数过多,怎么办?
在前期写的简单代理中,放在服务器上跑了一天,MongoDB 就不断重启,一开始以为是 MongoDB 的事情,但后来看见不对啊, TCP 链接数还是猛涨,因为服务器上用 MongoDB 的不止 NodeJS 的项目,还有 Python 的,所以排查很麻烦。
先优化了 Python 的,后来发现问题还在,关了 NodeJS 的项目,世界马上清净了。
那就是 NodeJS 链接的问题了。
廿贰-NodeJS简单代理池(承) 以及 Python 捉取网页内容
2。原
我完全根据官网 MongoDB 的 NodeJS 驱动来写的:
MongoClient.connect(that.strMGUrl, function(err, client) {
if (!err) {
const db = client.db(that.dbSet.dbName);
let curFind = db.collection(that.dbSet.col).find({
})
curFind.toArray(function(err, item) {
client.logout();
client.close();
curFind.close();
if (!err) {
if (!!item) {
let arrProxyBack = [];
for (const objProxyInMDB of item) {
arrProxyBack.push(common.funObj2Str(objProxyInMDB));
}
funCB(null, arrProxyBack);
} else {
funCB(null, false);
};
} else {
funCB('error:in "loadEveryProxyMongodb" after find, err : ' + err);
};
});
} else {
client.logout();
client.close();
funCB('error:in "loadEveryProxyMongodb" after connect, err : ' + err);
};
});
标准的按照官网链接文章来写。
结果:
对,就是这个结果,明明我已经断开了呀!
我觉得可能链接方式不对,于是不断上网找链接方式,直到找到:
https://www.v2ex.com/amp/t/591315
有人说:
正确姿势是连着不关。官方 DEMO 只是展示了一次操作完整的过程,并不是说每次都要连接和关闭。
我瞬间就崩溃了。
3。壹改
首先我就按连着的来呗。
// 在构造类时
class cMongodbIO {
/** *Creates an instance of cMongodbIO. * @param {Object} objDBSet * @memberof cMongodbIO */
constructor(objDBSet) {
const that = this;
that.dbSet = objDBSet;
that.strMGUrl = 'mongodb://' + that.dbSet.user + ':' + that.dbSet.pw + '@localhost:' + that.dbSet.host + '/' + that.dbSet.dbName;
that.clientMongo = null;
MongoClient.connect(that.strMGUrl, {
poolSize: 50, keepAlive: false, reconnectTries: 100, reconnectInterval: 30, useNewUrlParser: true }, function(err, client) {
if (!err) {
that.clientMongo = client;
} else {
console.log('error:in connect to MongoDB, err : ' + err);
};
});
};
checkOnline(funCB) {
const that = this;
if (!that.clientMongo) {
funCB(null, false);
} else {
funCB(null, true);
}
};
....
};
//主函数中
function funInit() {
let strConfig = fs.readFileSync(path.join(__dirname, '/cfg.json'), {
encoding: "utf-8" });
let objConfig = JSON.parse(strConfig);
let arrSaveSet = objConfig.saveSet;
let objWebCfg = objConfig.webCfg;
let intCfgMaxFailTime = objConfig.MaxFailTime;
objCTLIO = new cControllerIO(arrSaveSet, intCfgMaxFailTime);
....
funStarMain();
function funStarMain() {
objCTLIO.checkOnline(function(err, bolResult) {
if (bolResult) {
console.log(' 完成初始化 ');
funGoPro();
funVerifyProxy();
funGoRandPro();
} else {
intRetry++;
console.log(' 第 ' + intRetry + ' 次尝试启动');
setTimeout(funStarMain, 3000);
};
});
};
};
诶!结果好很多诶!
在情人节当天看到这个结果我还是很感觉欣慰的。
但是,还是看到会有所抬升,为什么会这样呢!?
我思来想去,是不是闭包影响?导致那些 cursor 一直占着不释放?
3。贰改
// 主程序
function funVerifyProxy() {
objCTLIO.loadEveryProxy((err, result) => {
console.log((new Date().toString()) + ' 列出完所有代理。');
objCTSpy.verifyTheProxy(result, objCTLIO, (err, result) => {
console.log((new Date().toString()) + ' 验证完所有了。');
objCTLIO.saveTick((err, result) => {
console.log((new Date().toString()) + ' 保存完。');
setTimeout(() => {
timeToVerifyProxy(); // <---原本这里是直接调用原函数的
}, (1000 * 60 * 60 * (objTimeConfig.verify[0] + Math.random() * objTimeConfig.verify[1])));
});
});
});
};
function timeToVerifyProxy() {
funVerifyProxy();
};
原本我是直接在 setTimeout里面调用回自身函数,我猜测会不会是因为闭包的原因它一直不释放 cursor 的内存呢?
修改后看到:
Oh!已经好了一点!
(虽然我还是不太懂这个是不是真的没有把前期的闭包相关变量包进去,但是明显好了一点)
但,为什么还是有爬升呢?
4。肆改
再又查了好久文档之后,看到:

因为一个英文的问题,我一致以为那个 noCursorTimeout 是 “没有 cursor 的时候 / 超时断开”,结果后来才发现,那个原来的意思是 “没有 / cursor 连接超时断开”
断句的问题很恐怖!
改为:
let curFind = db.collection(that.dbSet.col).find({
}, {
'noCursorTimeout': false, 'timeout': true })
curFind.toArray(function(err, item) {
// client.logout();
// client.close();
curFind.close();
if (!err) {
if (!!item) {
let arrProxyBack = [];
for (const objProxyInMDB of item) {
arrProxyBack.push(common.funObj2Str(objProxyInMDB));
}
funCB(null, arrProxyBack);
} else {
funCB(null, false);
};
} else {
funCB('error:in "loadEveryProxyMongodb" after find, err : ' + err);
};
});
呃,我还是先放个十几个小时看看结果吧。
5。后事
多亏腾讯云有各种的监控我懒得自己建监控东西。
希望这次能解决问题吧,我觉得一类文章写个“起转承合”已经不想再写了,我想放弃了。
虽然其实事情还没解决,唉,我很懒啊…
边栏推荐
- 数学公式显示
- 自定义失败处理
- 77.5% of the world's websites are using PHP, the "best language in the world"!
- Today in history: PHP is publicly released; IPhone 4 comes out; The birth of the father of the World Wide Web
- Solutions to some medium and high risk vulnerabilities detected by appscan
- 基于配置的权限控制
- 同花顺app交易安全吗
- Machine learning housing rental price forecasting: exploratory data analysis + Feature Engineering + modeling + reporting
- 1331. array sequence number conversion - quick sort plus binary search
- 剑指 Offer 19. 正则表达式匹配-递归
猜你喜欢

安防监控视频EasyCVR视频调阅界面增加单个视频的关闭按钮

Mysql数据库头歌实训任务存储过程如何通关?

106. construct binary tree from middle order and post order traversal sequence

15 must know MySQL index failure scenarios, stop stepping on the pit!

Key configuration points of video fusion cloud service easycvr platform deployed in ECS

Interaction between C language and Lua (practice 2)

77.5% of the world's websites are using PHP, the "best language in the world"!

【图像增强】基于稀疏表示和正则化实现图像增强附matlab代码

How many points can you get if the programmer's college entrance examination paper is exposed?

TensorFlow新文档发布:新增CLP、DTensor...最先进的模型已就绪!
随机推荐
eBPF 在云原生环境中的应用
中信建投是安全的吗
【水果识别】基于形态学实现水果识别含Matlab源码
MicroNet:以极低的 FLOP 实现图像识别
随时随地可访问的 IT 资源构成
Micronet: image recognition with very low flop
Interview question 10.03 Search rotation array
Configuration based permission control
【PHP】代码复用特殊类Trait的简要说明和相关举例
Implementing recurrent neural networks from scratch -- [torch learning notes]
TensorFlow新文档发布:新增CLP、DTensor...最先进的模型已就绪!
数学公式显示
349. 两个数组的交集
人大金仓再次荣获“国家鼓励的重点软件企业”认定
1331. 数组序号转换-快速排序加二分查找
Terrain learning summary (6) -- terrain practice based on Alibaba cloud platform
leetcode. 36 --- effective Sudoku
什么样的数字藏品平台才是好平台?
全球 77.5% 的网站,都在使用“世界上最好的语言” PHP!
Mingdao cloud on the list of office software in China's information and innovation industry in 2022