Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pool settings ignored with mongoDB #2228

Closed
edorgeville opened this issue Sep 23, 2021 · 4 comments
Closed

pool settings ignored with mongoDB #2228

edorgeville opened this issue Sep 23, 2021 · 4 comments
Assignees

Comments

@edorgeville
Copy link

Describe the bug
I am trying to limit the number of concurrent connections. I used the pool parameter for MikroORM.init, but it doesn't seem to be used. Instead, 11 connections are opened. I am guessing one is the main em, the 10 others being each RequestContext, but then those connections get reused (?).

To Reproduce

import { MikroORM, RequestContext } from '@mikro-orm/core'
import { MongoDriver } from '@mikro-orm/mongodb'
import { User } from './entities/User'
import { name, random } from 'faker'
import delay from 'delay'

const entrypoint = async () => {
  const orm = await MikroORM.init<MongoDriver>({
    entities: [User],
    dbName: 'main',
    type: 'mongo',
    implicitTransactions: false,
    forceEntityConstructor: true,
    pool: {
      min: 1,
      max: 1
    }
  })

  // Cleanup
  await orm.em.nativeDelete(User, {})


  console.log('Create a user in 500 setImmediate')
  for (let i = 0; i < 500; i++) {
    setImmediate(async () => {
      await RequestContext.createAsync(orm.em, async () => {
        const user = new User()
        user.name = name.findName()
        console.log(i, user)
        await RequestContext.getEntityManager().persistAndFlush(user)
      })
    })
  }

  await delay(100)
  await orm.close()
}


entrypoint()

MongoDB logs

I  NETWORK  [listener] connection accepted from 172.27.0.1:59734 #1 (1 connection now open)
I  NETWORK  [conn1] received client metadata from 172.27.0.1:59734 conn1: { driver: { name: "nodejs", version: "3.6.6" }, os: { type: "Windows_NT", name: "win32", architecture: "x64", version: "10.0.19042" }, platform: "'Node.js v14.17.3, LE (unified)" }      
I  NETWORK  [listener] connection accepted from 172.27.0.1:59738 #2 (2 connections now open)
I  NETWORK  [conn2] received client metadata from 172.27.0.1:59738 conn2: { driver: { name: "nodejs", version: "3.6.6" }, os: { type: "Windows_NT", name: "win32", architecture: "x64", version: "10.0.19042" }, platform: "'Node.js v14.17.3, LE (unified)" }      
I  SHARDING [conn2] Marking collection main.user as collection version: <unsharded>
I  NETWORK  [listener] connection accepted from 172.27.0.1:59742 #3 (3 connections now open)
I  NETWORK  [listener] connection accepted from 172.27.0.1:59746 #4 (4 connections now open)
I  NETWORK  [listener] connection accepted from 172.27.0.1:59750 #5 (5 connections now open)
I  NETWORK  [listener] connection accepted from 172.27.0.1:59754 #6 (6 connections now open)
I  NETWORK  [listener] connection accepted from 172.27.0.1:59758 #7 (7 connections now open)
I  NETWORK  [listener] connection accepted from 172.27.0.1:59762 #8 (8 connections now open)
I  NETWORK  [listener] connection accepted from 172.27.0.1:59768 #9 (9 connections now open)
I  NETWORK  [listener] connection accepted from 172.27.0.1:59770 #10 (10 connections now open)
I  NETWORK  [listener] connection accepted from 172.27.0.1:59774 #11 (11 connections now open)
I  NETWORK  [conn1] end connection 172.27.0.1:59734 (10 connections now open)
I  NETWORK  [conn3] end connection 172.27.0.1:59742 (9 connections now open)
I  NETWORK  [conn4] end connection 172.27.0.1:59746 (8 connections now open)
I  NETWORK  [conn8] end connection 172.27.0.1:59762 (7 connections now open)
I  NETWORK  [conn5] end connection 172.27.0.1:59750 (6 connections now open)
I  NETWORK  [conn11] end connection 172.27.0.1:59774 (5 connections now open)
I  NETWORK  [conn7] end connection 172.27.0.1:59758 (4 connections now open)
I  NETWORK  [conn6] end connection 172.27.0.1:59754 (3 connections now open)
I  NETWORK  [conn9] end connection 172.27.0.1:59768 (2 connections now open)
I  NETWORK  [conn10] end connection 172.27.0.1:59770 (1 connection now open)
I  NETWORK  [conn2] end connection 172.27.0.1:59738 (0 connections now open)

Expected behavior
I expected a single connection to be created and used.

Versions

Dependency Version
node 14.17.3
typescript 4.3.4
@mikro-orm/core 4.5.6
@mikro-orm/mongodb 4.5.6
@B4nan
Copy link
Member

B4nan commented Sep 23, 2021

That is a knex configuration (better say tarn configuration), not used in mongo driver at all. Based on the mongodb typings, it looks like the options are called poolSize and minSize (or maxPoolSize and minPoolSize, not sure what is the real difference for us).

You can use driverOptions to set anything that is not directly supported in the ORM configuration.

But based on the docs, the default pool size should be 5, so it sounds weird you would be getting 11 connections. But even if that is not working, it would be an upstream issue.

@edorgeville
Copy link
Author

As always, thanks for the blazing fast answer! 🚀
Adding driverOptions: { poolSize: n } to the MikroORM.init<MongoDriver> options seems to work. Oddly, there seems to always be n+1 connections. I'll look deeper into this and report back with my findings. Might just be user error 😅

@B4nan
Copy link
Member

B4nan commented Sep 23, 2021

Let's keep this open, we can definitely map the pool: {min, max} to the mongo connection settings. Can you please verify whether we want the poolSize and minSize, or the maxPoolSize and minPoolSize? I guess it should be safe to set both of them at the same time?

@B4nan B4nan reopened this Sep 23, 2021
@edorgeville
Copy link
Author

edorgeville commented Sep 23, 2021

I can confirm that driverOptions: { minPoolSize: n, maxPoolSize: n } work identically. Still have the n+1 issue but let's put this aside for now.
minPoolSize and maxPoolSize seem to be documented properly, but I can't find minSize nor poolSize. Possibly deprecated?

@B4nan B4nan closed this as completed in 9223055 Sep 23, 2021
Langstra pushed a commit to RiskChallenger/mikro-orm that referenced this issue Nov 8, 2021
Langstra pushed a commit to RiskChallenger/mikro-orm that referenced this issue Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants