Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): scope the ingress ordering lock per plugin

Yudhi Armyndharis28954bc1a937
diff --git a/src/modules/integration/dto/instance.dto.ts b/src/modules/integration/dto/instance.dto.ts
index 0e3eab06d..0ad1ce0a6 100644
--- a/src/modules/integration/dto/instance.dto.ts
+++ b/src/modules/integration/dto/instance.dto.ts
@@ -13,7 +13,7 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IngressUrl } from '../ingress-url';
import { ToStrictBoolean } from '../../../common/utils/strict-boolean';
-// Safe charset: also prevents an instanceId containing ':' (which would collide the P1 ordering key).
+// Safe charset: the instanceId goes unencoded into the ingress URL path and the `<pluginId>:<instanceId>` row id.
const INSTANCE_ID_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
export class CreateInstanceDto {
diff --git a/src/modules/integration/ordering-lock.spec.ts b/src/modules/integration/ordering-lock.spec.ts
index 65941e534..e5d030d78 100644
--- a/src/modules/integration/ordering-lock.spec.ts
+++ b/src/modules/integration/ordering-lock.spec.ts
@@ -56,8 +56,8 @@ describe('orderingKeyFor', () => {
};
}
- it('keys on instanceId + providerConversationId when present', () => {
- expect(orderingKeyFor(job({ providerConversationId: 'c1' }))).toBe('acct1:c1');
+ it('keys on pluginId + instanceId + providerConversationId when present', () => {
+ expect(orderingKeyFor(job({ providerConversationId: 'c1' }))).toBe('chatwoot\u0000acct1\u0000c1');
});
it('two jobs with the same providerConversationId share a key', () => {
@@ -73,7 +73,7 @@ describe('orderingKeyFor', () => {
});
it('falls back to a per-instance key when providerConversationId is absent', () => {
- expect(orderingKeyFor(job())).toBe('instance:acct1');
+ expect(orderingKeyFor(job())).toBe('chatwoot\u0000acct1\u0000');
});
it('two jobs with no providerConversationId on the same instance share the fallback key', () => {
@@ -82,6 +82,21 @@ describe('orderingKeyFor', () => {
expect(a).toBe(b);
});
+ it('two plugins sharing an instanceId do not share a key, with or without a conversation', () => {
+ expect(orderingKeyFor(job({ pluginId: 'chatwoot', instanceId: 'prod' }))).not.toBe(
+ orderingKeyFor(job({ pluginId: 'typebot', instanceId: 'prod' })),
+ );
+ expect(orderingKeyFor(job({ pluginId: 'chatwoot', instanceId: 'prod', providerConversationId: 'c1' }))).not.toBe(
+ orderingKeyFor(job({ pluginId: 'typebot', instanceId: 'prod', providerConversationId: 'c1' })),
+ );
+ });
+
+ it("a conversation key never equals another instance's per-instance key", () => {
+ expect(orderingKeyFor(job({ instanceId: 'instance', providerConversationId: 'prod' }))).not.toBe(
+ orderingKeyFor(job({ instanceId: 'prod' })),
+ );
+ });
+
it('never keys on deliveryId', () => {
const a = orderingKeyFor(job({ deliveryId: 'd1', providerConversationId: 'c1' }));
const b = orderingKeyFor(job({ deliveryId: 'd2', providerConversationId: 'c1' }));
diff --git a/src/modules/integration/ordering-lock.ts b/src/modules/integration/ordering-lock.ts
index 815e73223..fef2f2ffb 100644
--- a/src/modules/integration/ordering-lock.ts
+++ b/src/modules/integration/ordering-lock.ts
@@ -23,10 +23,11 @@ export class KeyedAsyncLock {
// Key on the CONVERSATION, never the deliveryId (a delivery-unique key would serialize nothing).
// providerConversationId is populated host-side when the plugin manifest declares a conversationId
-// pointer; absent it, we serialize per instance — correct (never reorders within a conversation) but
-// coarse. Per-instance fallback is the lazy-correct default; the conversation key unlocks
-// intra-instance parallelism once the manifest declares the pointer.
+// pointer; absent it, we serialize per instance: correct (never reorders within a conversation) but
+// coarse. The conversation key unlocks intra-instance parallelism once the manifest declares the
+// pointer. The key is scoped per (plugin, instance): an instanceId is unique only within its plugin,
+// so two plugins may both name an instance 'prod' and must not serialize each other. NUL-joined so
+// no choice of ids can make one tuple read as another; an absent conversation is the empty field.
export function orderingKeyFor(job: IngressJobData): string {
- if (job.providerConversationId) return `${job.instanceId}:${job.providerConversationId}`;
- return `instance:${job.instanceId}`;
+ return [job.pluginId, job.instanceId, job.providerConversationId ?? ''].join('\u0000');
}