Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(pacing): charge a midnight-straddling group reachout to the new day

Yudhi Armyndharisb066e7265fbd
diff --git a/src/modules/message/send-pacing-cold-query.spec.ts b/src/modules/message/send-pacing-cold-query.spec.ts
index ef386dcf5..5eeef1c1a 100644
--- a/src/modules/message/send-pacing-cold-query.spec.ts
+++ b/src/modules/message/send-pacing-cold-query.spec.ts
@@ -343,19 +343,33 @@ describe('group reachouts against a real database', () => {
await expect(reachout('s1', ['[email protected]'])).rejects.toMatchObject({ status: 429 });
});
- it("does not let a check that straddled UTC midnight overwrite the new day's tally", async () => {
+ // A check whose count query spans UTC midnight is judged by, and charged to, the new day.
+ const rollOverDuringCount = (during: () => Promise<unknown> = async () => undefined): void => {
const internals = service as unknown as { countColdReachoutsToday: () => Promise<number> };
const realCount = internals.countColdReachoutsToday.bind(service);
- // While the first check is suspended on its count query, the clock rolls over and another
- // request spends the whole new day's allowance.
- jest.spyOn(internals, 'countColdReachoutsToday').mockImplementationOnce(async () => {
- jest.setSystemTime(new Date(NOW.getTime() + DAY_MS));
- await reachout('s1', ['[email protected]', '[email protected]', '[email protected]']);
- return 0;
- });
- jest.spyOn(internals, 'countColdReachoutsToday').mockImplementation(realCount);
+ jest
+ .spyOn(internals, 'countColdReachoutsToday')
+ .mockImplementationOnce(async () => {
+ jest.setSystemTime(new Date(NOW.getTime() + DAY_MS));
+ await during();
+ return 0;
+ })
+ .mockImplementation(realCount);
+ };
+
+ it('charges a check that straddled UTC midnight to the new day', async () => {
+ rollOverDuringCount();
- await reachout('s1', ['[email protected]']);
+ await reachout('s1', ['[email protected]', '[email protected]', '[email protected]']);
+ await expect(reachout('s1', ['[email protected]'])).rejects.toMatchObject({ status: 429 });
+ });
+
+ it("refuses a straddling check once another request spent the new day's allowance", async () => {
+ // While the first check is suspended on its count query, another request spends the whole
+ // new day's allowance.
+ rollOverDuringCount(() => reachout('s1', ['[email protected]', '[email protected]', '[email protected]']));
+
+ await expect(reachout('s1', ['[email protected]'])).rejects.toMatchObject({ status: 429 });
await expect(reachout('s1', ['[email protected]'])).rejects.toMatchObject({ status: 429 });
});
diff --git a/src/modules/message/send-pacing.service.ts b/src/modules/message/send-pacing.service.ts
index 05342dd02..0f35f44c5 100644
--- a/src/modules/message/send-pacing.service.ts
+++ b/src/modules/message/send-pacing.service.ts
@@ -217,6 +217,11 @@ export class SendPacingService {
// against themselves and the cap would reset every request.
const usedToday =
(await this.countColdReachoutsToday(sessionId, dayStart)) + this.groupReachoutsToday(sessionId, dayStart);
+ // The UTC day rolled over while counting: check again against the new day, so the batch is
+ // reserved on (and judged by) the day it actually runs in.
+ if (startOfUtcDay(new Date()).getTime() !== dayStart.getTime()) {
+ return this.assertReachoutAllowed(sessionId, contactIds);
+ }
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
@@ -258,9 +263,6 @@ export class SendPacingService {
private addGroupReachouts(sessionId: string, dayStart: Date, n: number): void {
const dayStartMs = dayStart.getTime();
const tally = this.groupReachoutTally.get(sessionId);
- // A check that straddled midnight must not replace a tally another request already started
- // for the new day.
- if (tally && tally.dayStartMs > dayStartMs) return;
if (tally && tally.dayStartMs === dayStartMs) tally.count += n;
else this.groupReachoutTally.set(sessionId, { dayStartMs, count: n });
}