Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): answer 429 when the bulk batch cap is reached
Yudhi Armyndharis30bcc5c6db4b
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.tsindex f6ae7f95f..5e209038b 100644--- a/src/modules/message/bulk-message.service.spec.ts+++ b/src/modules/message/bulk-message.service.spec.ts@@ -381,7 +381,10 @@ describe('BulkMessageService.processBatch', () => {repo.findOne.mockResolvedValue(null); // batchId not taken(service as unknown as { inFlightBatches: number }).inFlightBatches = 2; // at capconst dto = { messages: [{ chatId: '[email protected]', type: 'text', content: { text: 'hi' } }] };- await expect(service.createBatch('s1', dto as never)).rejects.toThrow(/too many bulk batches/i);+ const create = service.createBatch('s1', dto as never);+ await expect(create).rejects.toThrow(/too many bulk batches/i);+ // A capacity refusal, not a malformed request: 429 is what clients retry on.+ await expect(create).rejects.toMatchObject({ status: 429 });expect(repo.save).not.toHaveBeenCalled(); // rejected before a PENDING row is written} finally {if (prev === undefined) delete process.env.BULK_MAX_CONCURRENT_BATCHES;diff --git a/src/modules/message/bulk-message.service.ts b/src/modules/message/bulk-message.service.tsindex b7d452ea4..88fe128fd 100644--- a/src/modules/message/bulk-message.service.ts+++ b/src/modules/message/bulk-message.service.ts@@ -1,4 +1,12 @@-import { Injectable, BadRequestException, NotFoundException, Optional, OnApplicationBootstrap } from '@nestjs/common';+import {+ Injectable,+ BadRequestException,+ HttpException,+ HttpStatus,+ NotFoundException,+ Optional,+ OnApplicationBootstrap,+} from '@nestjs/common';import { createLogger } from '../../common/services/logger.service';import { ConfigService } from '@nestjs/config';import { InjectRepository } from '@nestjs/typeorm';@@ -247,7 +255,10 @@ export class BulkMessageService implements OnApplicationBootstrap {// hold an unbounded number of full message sets (base64 media included) in memory at once.const maxConcurrentBatches = resolveMaxConcurrentBatches();if (maxConcurrentBatches > 0 && this.inFlightBatches >= maxConcurrentBatches) {- throw new BadRequestException(`Too many bulk batches in progress (max ${maxConcurrentBatches}); retry shortly`);+ throw new HttpException(+ `Too many bulk batches in progress (max ${maxConcurrentBatches}); retry shortly`,+ HttpStatus.TOO_MANY_REQUESTS,+ );}const options = {delayBetweenMessages: dto.options?.delayBetweenMessages ?? 3000,