Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(pacing): hint the hold lapse on a group add refused by held sends

Yudhi Armyndharis8300de41b1ec
diff --git a/src/modules/message/send-pacing-cold-query.spec.ts b/src/modules/message/send-pacing-cold-query.spec.ts
index 9e95bde17..4cdb43f00 100644
--- a/src/modules/message/send-pacing-cold-query.spec.ts
+++ b/src/modules/message/send-pacing-cold-query.spec.ts
@@ -653,6 +653,19 @@ describe('concurrent sends against a real database', () => {
});
});
+ it('answers a group add refused only by held cold sends with the seconds until they lapse', async () => {
+ const service = build([10_000], [5]);
+ for (let i = 0; i < 3; i++) expect(await send(service, `first${i}@c.us`, i)).toBe(true);
+ // A cold send admitted but never persisted (its outcome unknown), so it stays held.
+ await service.assertSendAllowed('s1', '[email protected]');
+ jest.setSystemTime(NOW.getTime() + 4_000);
+
+ await expect(service.assertReachoutAllowed('s1', ['[email protected]', '[email protected]'])).rejects.toMatchObject({
+ status: 429,
+ response: { code: SEND_PACING_LIMITED, retryAfterSeconds: 6 },
+ });
+ });
+
it('judges by the persisted rows alone once the hold has lapsed', async () => {
const service = build([5], []);
// A gated send that never writes a row (a plugin veto) is held only briefly.
diff --git a/src/modules/message/send-pacing.service.ts b/src/modules/message/send-pacing.service.ts
index 28a549e99..b1701073b 100644
--- a/src/modules/message/send-pacing.service.ts
+++ b/src/modules/message/send-pacing.service.ts
@@ -357,8 +357,8 @@ export class SendPacingService {
if (startOfUtcDay(new Date()).getTime() !== dayStart.getTime()) {
return this.assertReachoutAllowed(sessionId, contactIds);
}
- const usedToday =
- this.chatReachoutsToday(sessionId, dayStart, coldChats) + this.groupReachoutsToday(sessionId, dayStart);
+ const groupToday = this.groupReachoutsToday(sessionId, dayStart);
+ const usedToday = this.chatReachoutsToday(sessionId, dayStart, coldChats) + groupToday;
if (usedToday + coldCount <= allowance) {
// Reserved now, with no await between the check and the charge: a concurrent request must
// see this batch as spent. The caller refunds it (refundGroupReachouts) if the engine call
@@ -368,7 +368,12 @@ export class SendPacingService {
return { coldCount, dayStartMs: dayStart.getTime() };
}
- this.refuse('cold_daily_cap', sessionId, secondsUntilNextUtcDay(), {
+ // Refused only because of cold sends still held: the batch fits once they lapse, not at the next UTC day.
+ const retryAfter =
+ coldChats + groupToday + coldCount <= allowance
+ ? secondsUntilLapsed(this.heldAdmissions(sessionId, dayStart).filter(a => a.cold))
+ : secondsUntilNextUtcDay();
+ this.refuse('cold_daily_cap', sessionId, retryAfter, {
reason:
`Reaching ${coldCount} new contact(s) would exceed the daily allowance of ${allowance} ` +
`new conversation(s) for a session ${ageDays} day(s) old (${usedToday} already used)`,