refactor: impl counter based event buffer

1. counter based event buffer keeps track of the number of blocks that request event processing in buffer
2. event buffer only releases events when the last block completes (i.e. counter = 0)

this approach is far simpler than the promised based garbled crap I wrote, should also prevent the deadlock issues it introduced 🙏
This commit is contained in:
Adhiraj Singh
2022-12-02 11:31:42 +05:30
parent b520d81968
commit 30e2cb5c4c
5 changed files with 78 additions and 136 deletions

View File

@@ -645,16 +645,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
identifier: string,
exec: (node: BinaryNode) => Promise<any>
) => {
const started = ev.buffer()
if(started) {
await execTask()
if(started) {
await ev.flush()
}
} else {
const task = execTask()
ev.processInBuffer(task)
}
ev.buffer()
await execTask()
ev.flush()
function execTask() {
return exec(node)
@@ -662,17 +655,6 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
}
// called when all offline notifs are handled
ws.on('CB:ib,,offline', async(node: BinaryNode) => {
const child = getBinaryNodeChild(node, 'offline')
const offlineNotifs = +(child?.attrs.count || 0)
logger.info(`handled ${offlineNotifs} offline messages/notifications`)
await ev.flush()
ev.emit('connection.update', { receivedPendingNotifications: true })
})
// recv a message
ws.on('CB:message', (node: BinaryNode) => {
processNodeWithBuffer(node, 'processing message', handleMessage)