Huge amount of connections

Hello,
I have a huge amount of connections not closing, I tried a lot of actions to make, but nothing changed, connections are always > 80%.

I have Nextjs application and MongoDB connection. I am deploying my nextjs app with Vercel, so each time I push to git branch vercel creates new “version” of app - maybe problem is here.

I have bought M10 subscription to have high performance, but each day I am receiving notification “Connections % of configured limit has gone above 80” - but I have only 3 users working (test platform).

What I tried to do:

  1. Pause cluster, to close all connections - didn’t help
  2. According to docs, I cant set whitelist because of Vercel - reading docs doesn’t help
  3. Rework my code of middleware (maybe problem is here) - code reworked, but nothing changes.
    Here is example of the code I have tested:
  import { MongoClient } from 'mongodb';
  import nextConnect from 'next-connect';

  const mongoClient = new MongoClient(process.env.mongoApiUrl, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });

  //with serverless we need to use cache to prevent re-opening connection
  let cached = global.mongo


  if (!cached) {
    cached = global.mongo = { conn: null, promise: null }
  }

  async function database(req, res, next) {
    //console.log(cached.promise)
    if (!cached.promise) {
      cached.promise = mongoClient.connect().then((client) => {
        return {
          client,
          db: client.db(process.env.MONGODB_DB),
        }
      })
      cached.conn = await cached.promise
    }

    req.dbClient = cached.conn.client
    req.db = cached.conn.db

    return next();
  }

  const middleware = nextConnect();

  middleware.use(database);


  export default middleware;```

AND (I have tested also)



import { MongoClient } from 'mongodb';

const MONGODB_URI = process.env.mongoApiUrl;
const MONGODB_DB = process.env.MONGODB_DB;

// check the MongoDB URI
if (!MONGODB_URI) {
    throw new Error('Define the MONGODB_URI environmental variable');
}

// check the MongoDB DB
if (!MONGODB_DB) {
    throw new Error('Define the MONGODB_DB environmental variable');
}

let cachedClient = null;
let cachedDb = null;

export async function connectToDatabase() {
    // check the cached.
    if (cachedClient && cachedDb) {
        // load from cache
        return {
            client: cachedClient,
            db: cachedDb,
        };
    }

    // set the connection options
    const opts = {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    };

    // Connect to cluster
    let client = new MongoClient(MONGODB_URI, opts);
    await client.connect();
    let db = client.db(MONGODB_DB);

    // set cache
    cachedClient = client;
    cachedDb = db;

    return {
        client: cachedClient,
        db: cachedDb,
    };
}

The problem I see is that connections are not closing, even at nights where no users working and using DB.
Please help!

1 Like

Same issue here, did you find a solution?