feat: complete cmpp gateway delivery recovery workflows

This commit is contained in:
hectorzhao
2026-07-08 16:30:06 +08:00
parent cc628d0214
commit 8144f08652
60 changed files with 8901 additions and 94 deletions
+7 -1
View File
@@ -155,6 +155,8 @@ describe('ChannelsService', () => {
account: 'sp',
passwordCipher: 'secret',
srcId: '10690000',
desiredConnections: 2,
windowSize: 32,
});
await service.createGroup({ code: 'G-MOBILE', name: '移动组', carrier: 'mobile', retryEnabled: true, retryTimeLimitMinutes: 750 });
await service.createRouteRule({ tenantId: 'tenant-1', applicationId: 'app-1', groupId: 'group-1', carrier: 'mobile' });
@@ -166,6 +168,7 @@ describe('ChannelsService', () => {
rateLimitPerSecond: 100,
sendRegion: '全国',
status: 'active',
config: expect.objectContaining({ desiredConnections: 2, windowSize: 32 }),
}),
});
expect(prisma.cmppConnectionState.create).toHaveBeenCalledWith({
@@ -173,7 +176,7 @@ describe('ChannelsService', () => {
channelId: 'channel-1',
connectionId: 'channel-1:primary',
status: 'connecting',
desiredConnections: 1,
desiredConnections: 2,
currentConnections: 0,
}),
});
@@ -215,6 +218,8 @@ describe('ChannelsService', () => {
sendRegion: '全国',
account: 'sp-new',
srcId: '10690001',
desiredConnections: 3,
windowSize: 64,
unitPrice: 4,
})).resolves.toEqual(expect.objectContaining({
id: 'channel-1',
@@ -230,6 +235,7 @@ describe('ChannelsService', () => {
gatewayPort: 27890,
carrier: 'all',
passwordCipher: undefined,
config: expect.objectContaining({ desiredConnections: 3, windowSize: 64 }),
}),
});
expect(prisma.operationLog.create).toHaveBeenCalledWith({
+28 -2
View File
@@ -20,6 +20,8 @@ export interface CreateChannelDto {
rateLimitPerSecond?: number;
unitPrice?: number;
status?: string;
desiredConnections?: number;
windowSize?: number;
config?: Record<string, unknown>;
}
@@ -196,6 +198,7 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
if (!Number.isInteger(gatewayPort) || gatewayPort <= 0 || gatewayPort > 65535) {
throw new BadRequestException('gatewayPort must be an integer between 1 and 65535');
}
const config = normalizeChannelRuntimeConfig(data.config, data.desiredConnections, data.windowSize);
const channel = await this.prisma.smsChannel.create({
data: {
code: data.code,
@@ -213,7 +216,7 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
rateLimitPerSecond: data.rateLimitPerSecond ?? 100,
unitPrice: data.unitPrice ?? 0,
status: data.status ?? 'active',
config: data.config as Prisma.InputJsonValue | undefined,
config: config as Prisma.InputJsonValue,
},
});
if (channel.status === 'active') {
@@ -231,6 +234,9 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
if (gatewayPort !== undefined && (!Number.isInteger(gatewayPort) || gatewayPort <= 0 || gatewayPort > 65535)) {
throw new BadRequestException('gatewayPort must be an integer between 1 and 65535');
}
const config = data.config !== undefined || data.desiredConnections !== undefined || data.windowSize !== undefined
? normalizeChannelRuntimeConfig(channel.config, data.desiredConnections, data.windowSize)
: undefined;
const updated = await this.prisma.smsChannel.update({
where: { id: channelId },
data: {
@@ -249,7 +255,7 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
rateLimitPerSecond: data.rateLimitPerSecond,
unitPrice: data.unitPrice,
status: data.status,
config: data.config as Prisma.InputJsonValue | undefined,
config: config as Prisma.InputJsonValue | undefined,
},
});
await this.prisma.operationLog.create({
@@ -1105,6 +1111,26 @@ function getDesiredConnections(config?: Prisma.JsonValue | null) {
return 1;
}
function normalizeChannelRuntimeConfig(config?: Prisma.JsonValue | Record<string, unknown> | null, desiredConnections?: number, windowSize?: number) {
const base = config && typeof config === 'object' && !Array.isArray(config)
? { ...(config as Record<string, unknown>) }
: {};
base.desiredConnections = getPositiveRuntimeInteger(desiredConnections ?? base.desiredConnections, 1, 'desiredConnections');
base.windowSize = getPositiveRuntimeInteger(windowSize ?? base.windowSize, 16, 'windowSize');
return base;
}
function getPositiveRuntimeInteger(value: unknown, fallback: number, fieldName: string) {
if (value === undefined || value === null || value === '') {
return fallback;
}
const normalized = Number(value);
if (!Number.isInteger(normalized) || normalized <= 0) {
throw new BadRequestException(`${fieldName} must be a positive integer`);
}
return normalized;
}
function bullmqConnection() {
const redisUrl = new URL(process.env.REDIS_URL ?? 'redis://127.0.0.1:6379');
return {