Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(media): load a conversion's input inside the ffmpeg gate
Yudhi Armyndharis575b8201176f
diff --git a/src/modules/media/media-conversion.service.spec.ts b/src/modules/media/media-conversion.service.spec.tsindex 087f107c0..c2233cd08 100644--- a/src/modules/media/media-conversion.service.spec.ts+++ b/src/modules/media/media-conversion.service.spec.ts@@ -70,6 +70,37 @@ describe('MediaConversionService', () => {});afterEach(() => jest.restoreAllMocks());+ describe('input load', () => {+ // A download resolved before the gate was a 50 MiB buffer for every request that had not reached a+ // slot, held until the gate answered 503: peak memory was the number of requests, not the cap.+ it('downloads at most as many inputs at once as it converts', async () => {+ let downloading = 0;+ let peak = 0;+ jest.spyOn(loadRemoteMedia, 'loadRemoteMediaBuffer').mockImplementation(async () => {+ downloading++;+ peak = Math.max(peak, downloading);+ await new Promise(resolve => setTimeout(resolve, 15));+ downloading--;+ return { data: Buffer.from('input'), mimetype: 'audio/mpeg' };+ });+ runFfmpeg.mockImplementation(async () => {+ await new Promise(resolve => setTimeout(resolve, 15));+ return Buffer.from('converted-bytes');+ });+ const service = makeService(config({ 'mediaConversion.concurrency': 2 }));++ const outcomes = await Promise.allSettled(+ Array.from({ length: 30 }, () => service.convertToVoice(SESSION, { url: 'https://media.example/a.mp3' })),+ );++ expect(peak).toBeLessThanOrEqual(2);+ expect(outcomes.filter(o => o.status === 'fulfilled')).toHaveLength(10);+ const refused = outcomes.filter((o): o is PromiseRejectedResult => o.status === 'rejected');+ expect(refused).toHaveLength(20);+ expect(refused.every(o => o.reason instanceof ServiceUnavailableException)).toBe(true);+ });+ });+describe('availability', () => {it('refuses with 503 when conversion is switched off, without probing for the binary', async () => {const service = makeService(config({ 'mediaConversion.enabled': false }));diff --git a/src/modules/media/media-conversion.service.ts b/src/modules/media/media-conversion.service.tsindex 4a044452a..5dde475bf 100644--- a/src/modules/media/media-conversion.service.ts+++ b/src/modules/media/media-conversion.service.ts@@ -40,10 +40,10 @@ export class MediaConversionService implements OnApplicationShutdown {*/private binaryAvailable?: Promise<boolean>;/**- * Bounds concurrent ffmpeg processes (the rate limiter caps admission per second, not how many- * long-running processes stack up while each runs toward its timeout). Queue depth is a small- * multiple of the cap: each parked task holds its input buffer in heap, so anything beyond it- * answers 503 instead of accumulating.+ * Bounds concurrent conversions, input load included (the rate limiter caps admission per second,+ * not how many long-running processes stack up while each runs toward its timeout). Queue depth is+ * a small multiple of the cap, so anything beyond it answers 503 instead of accumulating; a parked+ * request holds only its own body, and at most the cap's worth of inputs sit in heap at once.*/private readonly ffmpegGate: ConcurrencyLimiter;@@ -97,16 +97,20 @@ export class MediaConversionService implements OnApplicationShutdown {outputMimetype: string,): Promise<ConvertedMedia> {await this.assertAvailable();- const input = await this.resolveInput(sessionId, dto);try {- const output = await this.ffmpegGate.run(() =>- runFfmpeg(input, 'bin', outputExtension, encodeArgs, {+ // The input is loaded inside the gate: a download of up to MEDIA_DOWNLOAD_MAX_BYTES, or a decoded+ // base64 body, held for each request that had not yet reached a slot, would stack 50 MiB a request+ // in heap while the gate answers 503 only after the downloads finished.+ const { input, output } = await this.ffmpegGate.run(async () => {+ const loaded = await this.resolveInput(sessionId, dto);+ const converted = await runFfmpeg(loaded, 'bin', outputExtension, encodeArgs, {ffmpegPath: this.configService.get<string>('mediaConversion.ffmpegPath', 'ffmpeg'),timeoutMs: this.configService.get<number>('mediaConversion.timeoutMs', 60_000),maxOutputBytes: this.configService.get<number>('mediaConversion.maxOutputBytes', 50 * 1024 * 1024),- }),- );+ });+ return { input: loaded, output: converted };+ });this.logger.log('Media converted', { inputBytes: input.length, outputBytes: output.length, outputMimetype });return { base64: output.toString('base64'), mimetype: outputMimetype, bytes: output.length };} catch (error) {