feat: limit number of message retries being sent

This commit is contained in:
Adhiraj Singh
2022-04-20 13:10:53 +05:30
parent 5824a0274d
commit af7b2a5dd2

View File

@@ -273,6 +273,11 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} }
} }
const willSendMessageAgain = (id: string) => {
const retryCount = msgRetryMap[id] || 0
return retryCount < 5
}
const sendMessagesAgain = async(key: proto.IMessageKey, ids: string[]) => { const sendMessagesAgain = async(key: proto.IMessageKey, ids: string[]) => {
const msgs = await Promise.all( const msgs = await Promise.all(
ids.map(id => ( ids.map(id => (
@@ -291,6 +296,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
for(let i = 0; i < msgs.length;i++) { for(let i = 0; i < msgs.length;i++) {
if(msgs[i]) { if(msgs[i]) {
msgRetryMap[ids[i]] = (msgRetryMap[ids[i]] || 0) + 1
await relayMessage(key.remoteJid, msgs[i], { await relayMessage(key.remoteJid, msgs[i], {
messageId: ids[i], messageId: ids[i],
participant participant
@@ -360,18 +366,22 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} }
if(attrs.type === 'retry') { if(attrs.type === 'retry') {
// correctly set who is asking for the retry if(willSendMessageAgain(key.id)) {
key.participant = key.participant || attrs.from // correctly set who is asking for the retry
if(key.fromMe) { key.participant = key.participant || attrs.from
try { if(key.fromMe) {
logger.debug({ attrs, key }, 'recv retry request') try {
await sendMessagesAgain(key, ids) logger.debug({ attrs, key }, 'recv retry request')
} catch(error) { await sendMessagesAgain(key, ids)
logger.error({ key, ids, trace: error.stack }, 'error in sending message again') } catch(error) {
shouldAck = false logger.error({ key, ids, trace: error.stack }, 'error in sending message again')
shouldAck = false
}
} else {
logger.info({ attrs, key }, 'recv retry for not fromMe message')
} }
} else { } else {
logger.info({ attrs, key }, 'recv retry for not fromMe message') logger.info({ attrs, key }, 'will not send message again, as sent too many times')
} }
} }