regexlaravelmongodblumen

How to close mongo db idle connections in lumen using jessengers?


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.


Solution

  • 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.