[英]MongoDB Change Streams and poolsize: how to share connection amongst request
实施变更流后,mongodb 的性能急剧下降。 现在我已经禁用了客户端上的 websocket,因为数据库无法处理这样的负载。 问题的根源在于已建立的数据库连接数。 也许可以在客户端之间共享相同的更改 stream 到 mongodb。
应用程序.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)
})
}
}
观看资料
// 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)
})
}
因此,每次客户端打开连接时,watchProfile 都会专门为此配置文件或用户实例化更改 stream。 但是,这对 100 多个用户来说是行不通的。 因此,我想问一下是否有一些可行的模式。 也许可以不为每个客户端实例化一个新连接,而只需准备一个 cursor 并在稍后阶段为每个用户过滤它。
已经尝试将 poolSize 增加到更大的数字(100),这在本地工作得很好,但在 MongoDB Atlas 的免费层上却不行。
更改流不需要专用连接 - 它们将在每个 getMore 操作期间使用池中的连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.