fix: minor retry logic cleanup

This commit is contained in:
Adhiraj Singh
2021-11-15 09:09:55 +05:30
parent 2dfe5adbe1
commit 9ed4c28b8e
2 changed files with 13 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
import { SocketConfig, WAMessageStubType, ParticipantAction, Chat, GroupMetadata } from "../Types" import { SocketConfig, WAMessageStubType, ParticipantAction, Chat, GroupMetadata, WAMessageKey } from "../Types"
import { decodeMessageStanza, encodeBigEndian, toNumber, downloadHistory, generateSignalPubKey, xmppPreKey, xmppSignedPreKey } from "../Utils" import { decodeMessageStanza, encodeBigEndian, toNumber, downloadHistory, generateSignalPubKey, xmppPreKey, xmppSignedPreKey } from "../Utils"
import { BinaryNode, jidDecode, jidEncode, isJidStatusBroadcast, areJidsSameUser, getBinaryNodeChildren, jidNormalizedUser, getAllBinaryNodeChildren, BinaryNodeAttributes } from '../WABinary' import { BinaryNode, jidDecode, jidEncode, isJidStatusBroadcast, areJidsSameUser, getBinaryNodeChildren, jidNormalizedUser, getAllBinaryNodeChildren, BinaryNodeAttributes } from '../WABinary'
import { proto } from "../../WAProto" import { proto } from "../../WAProto"
@@ -22,6 +22,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
sendDeliveryReceipt, sendDeliveryReceipt,
} = sock } = sock
const msgRetryMap = config.msgRetryCounterMap || { }
const sendMessageAck = async({ tag, attrs }: BinaryNode, extraAttrs: BinaryNodeAttributes) => { const sendMessageAck = async({ tag, attrs }: BinaryNode, extraAttrs: BinaryNodeAttributes) => {
const stanza: BinaryNode = { const stanza: BinaryNode = {
tag: 'ack', tag: 'ack',
@@ -38,14 +40,15 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
await sendNode(stanza) await sendNode(stanza)
} }
const retries = new Map<string, number>()
const sendRetryRequest = async(node: BinaryNode) => { const sendRetryRequest = async(node: BinaryNode) => {
if (retries.has(node.attrs.id) && retries.get(node.attrs.id)! >= 5) { const msgId = node.attrs.id
retries.delete(node.attrs.id) const retryCount = msgRetryMap[msgId] || 1
if(retryCount >= 5) {
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing')
delete msgRetryMap[msgId]
return return
} }
const retryCount = retries.get(node.attrs.id) || 1 msgRetryMap[msgId] = retryCount+1
retries.set(node.attrs.id, retryCount + 1)
const isGroup = !!node.attrs.participant const isGroup = !!node.attrs.participant
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds
@@ -59,7 +62,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const receipt: BinaryNode = { const receipt: BinaryNode = {
tag: 'receipt', tag: 'receipt',
attrs: { attrs: {
id: node.attrs.id, id: msgId,
type: 'retry', type: 'retry',
to: isGroup ? node.attrs.from : jidEncode(decFrom!.user, 's.whatsapp.net', decFrom!.device, 0) to: isGroup ? node.attrs.from : jidEncode(decFrom!.user, 's.whatsapp.net', decFrom!.device, 0)
}, },
@@ -102,9 +105,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} }
await sendNode(receipt) await sendNode(receipt)
logger.info({ msgId: node.attrs.id, retryCount }, 'sent retry receipt') logger.info({ msgAttrs: node.attrs, retryCount }, 'sent retry receipt')
ev.emit('auth-state.update', authState)
}) })
} }
const processMessage = async(message: proto.IWebMessageInfo, chatUpdate: Partial<Chat>) => { const processMessage = async(message: proto.IWebMessageInfo, chatUpdate: Partial<Chat>) => {

View File

@@ -50,6 +50,8 @@ export type SocketConfig = {
userDevicesCache?: NodeCache userDevicesCache?: NodeCache
/** provide a cache to store media, so does not have to be re-uploaded */ /** provide a cache to store media, so does not have to be re-uploaded */
mediaCache?: NodeCache mediaCache?: NodeCache
/** map to store the retry counts for failed messages */
msgRetryCounterMap?: { [msgId: string]: number }
} }
export enum DisconnectReason { export enum DisconnectReason {