我有一个使用 mongoengine 的 django 应用程序,它在 gunicorn 上与 gevent 工作人员一起运行。在负载下,mongo 连接数攀升至约 3000 并且永不下降。即使在负载测试完成后,mongo 连接数也保持不变。重新启动 gunicorn 会释放连接。
软件包版本
gunicorn==0.17.4
mongoengine==0.8.7
pymongo==2.7
mongodb 2.6.0
我在环境特定的 django 设置文件中有我的 mongoengine 连接设置:
MONGO_DATABASES = {
'default': {
'DB': '****',
'HOST': ***********:27017',
'PORT': 27017
}
}
from gevent import monkey
monkey.patch_all()
from mongoengine import connect
connect(MONGO_DATABASES['default']['DB'], host=MONGO_DATABASES['default']['HOST'], port=MONGO_DATABASES['default']['PORT'], max_pool_size=100)
我需要做些什么来确保最终释放未使用的连接吗?
谢谢,
道格
最佳答案
您正在寻找的功能是一个名为 maxIdleTimeMS
的配置选项,它旨在用于在负载高峰后进行清理(除其他外)。
根据文档:
maxIdleTimeMS - maximum time in millis a socket can sit idle in the pool before being closed and discarded. Useful for cleaning up after a load spike.
遗憾的是,此选项目前(2015 年 3 月)在 pymongo 驱动程序中不可用(mongoengine 在引擎盖下使用),但它正在开发中! See the relevant JIRA ticket (并且不要忘记支持这个问题!)。几个月后它应该可以在 pymongo 3.1 中使用。
关于python - Mongo 连接从未发布 - Django 和 Mongoengine 使用 gevent 在 gunicorn 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23833734/