简体   繁体   English

MongoDB 更改流和池大小:如何在请求之间共享连接

[英]MongoDB Change Streams and poolsize: how to share connection amongst request

After implementing change streams the performance of mongodb dropped dramatically.实施变更流后,mongodb 的性能急剧下降。 Now I've disabled the websockets on my client, because the database cannot handle such load.现在我已经禁用了客户端上的 websocket,因为数据库无法处理这样的负载。 The root of the problem is the number of connections to the database that has been made.问题的根源在于已建立的数据库连接数。 Maybe it is possible to share the same change stream to mongodb across clients.也许可以在客户端之间共享相同的更改 stream 到 mongodb。

App.ts应用程序.ts

const socketServer = require('https').createServer(sslOptions, app).listen(4001, () => {
  console.log('Socket.io Server running on port 4001')
})
const io = require('socket.io')(socketServer)

io.on('connection', (socket: Socket) => {
  const {profileId} = socket?.handshake?.query

  if (profileId) {
    watchProfile(profileId, (profile: ProfileDocument) => {
      socket.emit('updateProfile', profile)
    })
  }
}

Watch Profile观看资料

// Gets updates for a profile
export const watchProfile = (profileId: string, onChange: Function) => {
  Profile.watch(
    [{
      $match: {
        'fullDocument._id': ObjectId(profileId)
      }
    }], {
      fullDocument: 'updateLookup'
    }
  ).on('change', (data: any) => {
    const profile: ProfileDocument = data.fullDocument
    onChange(profile)
  })
}

So everytime a connection is opened by the client, watchProfile instantiates a change stream specifically for this profile or user.因此,每次客户端打开连接时,watchProfile 都会专门为此配置文件或用户实例化更改 stream。 However, this just doesn't work for 100s of users.但是,这对 100 多个用户来说是行不通的。 Therefore, I would like to ask if there are some patterns that will work.因此,我想问一下是否有一些可行的模式。 Maybe it is possible to not instantiate a new connection for every client, but just have one cursor ready and filter it for every user in a later stage.也许可以不为每个客户端实例化一个新连接,而只需准备一个 cursor 并在稍后阶段为每个用户过滤它。

Already attempted to increase the poolSize to a larger number (100), which worked very well locally but not on the free tier of MongoDB Atlas.已经尝试将 poolSize 增加到更大的数字(100),这在本地工作得很好,但在 MongoDB Atlas 的免费层上却不行。

Change streams do not require dedicated connections - they will use connections in the pool for the duration of each getMore operation.更改流不需要专用连接 - 它们将在每个 getMore 操作期间使用池中的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM