英文:
How to close mongo db idle connections in lumen using jessengers?
问题
我正在在Lumen中使用jessenger包进行与MongoDB的连接。
我遇到的问题是每个API都会打开一个新的MongoDB连接,这导致了服务器上大量的空闲数据库连接,浪费了资源并导致服务器变慢。
服务器正在使用php-fpm
。我尝试了pm=ondemand
与10秒的timeout
的解决方案,效果还不错,但在压力测试时失败了,因为应用是大规模的。
我正在寻找一个推荐的解决方案,已经有人测试过,可以关闭空闲的MongoDB连接。
英文:
I am using jessenger package for mongo db connection in lumen.
Issue I am facing is that each api open up a new mongo db connection. which turns out to be bulk of idle db connections of servers resulting wastage of resources and slowness of server.
Server is using php-fpm
. I have tried solution for pm=ondemand
with timeout
of 10 senconds. It worked quite good but failed the stress testing badly because application is largescale.
I am looking for a recommended solution which anyone has already tested to get the idle mongo db connections closed in mongo db.
答案1
得分: 1
在数据库连接字符串中使用以下标志以解决该问题:
disableClientPersistence
参见
[https://www.php.net/manual/en/mongodb-driver-manager.construct.php](https://www.php.net/manual/en/mongodb-driver-manager.construct.php)
> 如果为 true,则此 Manager 将使用一个新的 libmongoc 客户端,该客户端不会被持久化或与其他 Manager 对象共享。当释放此 Manager 对象时,其客户端将被销毁并关闭任何连接。默认为 false。
>
> 注意:通常不建议禁用客户端持久化。
英文:
In the db connection string use the following flag to resolve the issue:
disableClientPersistence
See
https://www.php.net/manual/en/mongodb-driver-manager.construct.php
> If true, this Manager will use a new libmongoc client, which will not be persisted or shared with other Manager objects. When this Manager object is freed, its client will be destroyed and any connections will be closed. Defaults to false.
>
> Note: Disabling client persistence is not generally recommended.
答案2
得分: 0
尝试在连接中使用 maxIdleTimeMS
参数。
'mongodb' => [
'options' => [
'connectTimeoutMS' => 30000, // 连接超时时间(以毫秒为单位)
'maxIdleTimeMS' => 60000, // 最大空闲时间(以毫秒为单位)
],
],
英文:
Try maxIdleTimeMS
in connection
'mongodb' => [
'options' => [
'connectTimeoutMS' => 30000, // Connection timeout (in milliseconds)
'maxIdleTimeMS' => 60000, // Mximum idle time (in milliseconds)
],
],
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论