Adjust data sync order when message event triggered
Created by: windmemory
Recently I was tweaking the wechaty-puppet-padpro
project, and found a sync issue related to message sync order.
Here is the code
public async ready (): Promise<void> {
log.verbose('Message', 'ready()')
if (this.isReady()) {
return
}
this.payload = await this.puppet.messagePayload(this.id)
if (!this.payload) {
throw new Error('no payload')
}
const fromId = this.payload.fromId
const roomId = this.payload.roomId
const toId = this.payload.toId
if (fromId) {
await this.wechaty.Contact.load(fromId).ready()
}
if (roomId) {
await this.wechaty.Room.load(roomId).ready()
}
if (toId) {
await this.wechaty.Contact.load(toId).ready()
}
}
Here is my case:
I logged in an account which has about 6k unread messages from several rooms, then I found thousands of
getContactPayload
call made to the weixin server which I think is scary. So I looked into the logic of how we load data, I found this piece of code above.
In this piece of code, when we receive a new message, we will sync all related data to it. But I found that when we sync data for the room and room members (two api calls), we will load the data for the toContact
and the fromContact
, then we don't need to sync it separately in underlying layer.
Today I encountered that there are 4k messages in one room, and sync up 20 messages one time. With the current logic, it will fire up 20 getContact
call to weixin server before it loads the room data. (Since the messages are fired up with event, so all these sync operations are parallel executed, thus 20 api call will be triggered together). I think it might be risky to do this, so I suggest to adjust the order of syncing data here to be room
-> fromContact
-> toContact