title: poolSize reaches the specified size , which causes application to slow down drastically
body: <p>I have 3 replica set running and I am also using the cluster module to fork  3 other processes ( the number of replica set created, does not have anything to do with the number of process forked ). In mongoose <code>connect</code> method i have the following option set</p>
  
  <pre><code>"use strict";
  
  const mongoose = require("mongoose");
  const config   = require("../config.js");
  // Set up mongoose connection
  
  mongoose.connect( config.mongoURI, {
  
      useNewUrlParser: true,
  
      // silent deprecation warning
      useCreateIndex: true,
  
      // auto reconnect to db
      autoReconnect: true,
  
      // turn off buffering, and fail immidiately mongodb disconnects
      bufferMaxEntries: 0,
      bufferCommands: false,
  
      keepAlive: true, 
      keepAliveInitialDelay: 450000,
  
      // number of socket connection to keep open
      poolSize: 1000
  }, error =&gt; {
      if (error) {
          console.log(error);
      }
  });
  
  
  module.exports = mongoose.connection;
  </code></pre>
  
  <p>The above code is in a file named <code>db.js</code>. In my server.js which starts the express application i require <code>db.js</code>. </p>
  
  <p>Whenever i reload the webpage multiple times it get's to a point were the app slows down to load drastically ( all this started happening when i decided to use a replica set ). I connected to mongdb through mongo shell and ran <code>db.serverStatus().connections</code> everytime i reloaded the page the <code>current</code> field increases ( which is what is expected anytime a new connection is made to mongodb ), but the problem is whenever the current field reaches the specified <code>poolSize</code> the application takes a lot of time to load. I tried calling <code>db.disconnect()</code> whenever the <code>end</code> event is emitted on the <code>req</code> express object, which will disconnect from mongodb ( this worked as expected but since i am using stream changes the above solution to close opend connections will throw <code>MongoError: Topology was destroyed</code>. The error been throwed is not the problem, the problem is preventing the app to slow down drastically if the currently opened connection hits the specified poolSize.</p>
  
  <p>I also tried setting <code>maxIdleTimeMS</code> in the mongodb connection string, and it is not working ( maybe mongoose does not support it )</p>
  
  <p><em>Note:</em> whenever i run <code>db.currentOps()</code> all active connections are set to <code>false</code></p>