Skip to content

Commit c9e00b7

Browse files
committed
fix(trigger): add trigger create rate limitation
1 parent 272f74a commit c9e00b7

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

src/modules/scf/entities/scf.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export default class ScfEntity extends BaseEntity {
148148
// 创建函数
149149
async create(inputs: ScfCreateFunctionInputs) {
150150
console.log(`Creating function ${inputs.name}, region ${this.region}`);
151-
const inp = formatInputs(this.region, inputs);
151+
const inp = formatInputs(inputs);
152152
const functionInputs = { Action: 'CreateFunction' as const, ...inp };
153153
await this.request(functionInputs);
154154
return true;
@@ -157,7 +157,7 @@ export default class ScfEntity extends BaseEntity {
157157
// 更新函数代码
158158
async updateCode(inputs: ScfCreateFunctionInputs, funcInfo: FunctionInfo) {
159159
console.log(`Updating function ${inputs.name} code, region ${this.region}`);
160-
const functionInputs = await formatInputs(this.region, inputs);
160+
const functionInputs = await formatInputs(inputs);
161161
const reqParams: UpdateFunctionCodeOptions = {
162162
Action: 'UpdateFunctionCode' as const,
163163
Handler: functionInputs.Handler || funcInfo.Handler,
@@ -175,7 +175,7 @@ export default class ScfEntity extends BaseEntity {
175175
// 更新函数配置
176176
async updateConfigure(inputs: ScfCreateFunctionInputs, funcInfo: FunctionInfo) {
177177
console.log(`Updating function ${inputs.name} configure, region ${this.region}`);
178-
let reqParams = await formatInputs(this.region, inputs);
178+
let reqParams = await formatInputs(inputs);
179179

180180
reqParams = {
181181
...reqParams,

src/modules/scf/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,21 @@ export default class Scf {
6262
return result;
6363
}
6464

65-
async getTriggerList(functionName: string, namespace = 'default'): Promise<TriggerType[]> {
65+
async getTriggerList(
66+
functionName: string,
67+
namespace = 'default',
68+
page = 0,
69+
): Promise<TriggerType[]> {
70+
const limit = 100;
6671
const { Triggers = [], TotalCount } = await this.request({
6772
Action: 'ListTriggers',
6873
FunctionName: functionName,
6974
Namespace: namespace,
7075
Limit: 100,
76+
Offset: page * limit,
7177
});
7278
if (TotalCount > 100) {
73-
const res = await this.getTriggerList(functionName, namespace);
79+
const res = await this.getTriggerList(functionName, namespace, page + 1);
7480
return Triggers.concat(res);
7581
}
7682

src/modules/scf/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { RegionType } from './../interface';
21
import { ScfCreateFunctionInputs, BaseFunctionConfig } from './interface';
32
const CONFIGS = require('./config').default;
43

54
// get function basement configure
65
// FIXME: unused variable region
7-
export const formatInputs = (region: RegionType, inputs: ScfCreateFunctionInputs) => {
6+
export const formatInputs = (inputs: ScfCreateFunctionInputs) => {
87
const functionInputs: BaseFunctionConfig = {
98
FunctionName: inputs.name,
109
Type: inputs.type === 'web' ? 'HTTP' : 'Event',

src/modules/triggers/manager.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { Capi } from '@tencent-sdk/capi';
2+
import { sleep } from '@ygkit/request';
13
import { ActionType } from '../scf/apis';
24
import { RegionType, ApiServiceType, CapiCredentials } from '../interface';
3-
import { Capi } from '@tencent-sdk/capi';
4-
import { ApiTypeError } from '../../utils/error';
5+
import { ApiError } from '../../utils/error';
56
import { deepClone } from '../../utils';
67
import TagsUtils from '../tag/index';
78
import ApigwUtils from '../apigw';
@@ -40,6 +41,11 @@ export class TriggerManager {
4041

4142
scf: ScfEntity;
4243

44+
// 当前正在执行的触发器任务数
45+
runningTasks = 0;
46+
// 支持并行执行的最大触发器任务数
47+
maxRunningTasks = 1;
48+
4349
constructor(credentials = {}, region: RegionType = 'ap-guangzhou') {
4450
this.region = region;
4551
this.credentials = credentials;
@@ -228,7 +234,6 @@ export class TriggerManager {
228234
// 1. 删除老的无法更新的触发器
229235
for (let i = 0, len = deleteList.length; i < len; i++) {
230236
const trigger = deleteList[i];
231-
232237
await this.removeTrigger({
233238
name,
234239
namespace,
@@ -243,12 +248,21 @@ export class TriggerManager {
243248
if (trigger?.NeedCreate === true) {
244249
const TriggerClass = TRIGGERS[Type];
245250
if (!TriggerClass) {
246-
throw new ApiTypeError('PARAMETER_SCF', `Unknown trigger type ${Type}`);
251+
throw new ApiError({
252+
type: 'PARAMETER_ERROR',
253+
message: `[TRIGGER] 未知触发器类型: ${Type}`,
254+
});
247255
}
248256
const triggerInstance = new TriggerClass({
249257
credentials: this.credentials,
250258
region: this.region,
251259
});
260+
// 针对触发器创建接口限频,由于后端服务问题,必须设置并发为 1
261+
// TODO: 兼容多个网关触发器并行部署时,服务发布会报错,待后端接口支持状态查询后再额外改造 apigw 模块
262+
this.runningTasks++;
263+
if (this.runningTasks > this.maxRunningTasks) {
264+
await sleep(1000);
265+
}
252266
const triggerOutput = await triggerInstance.create({
253267
scf: this,
254268
region: this.region,
@@ -258,6 +272,7 @@ export class TriggerManager {
258272
...trigger,
259273
},
260274
});
275+
this.runningTasks--;
261276

262277
deployList[i] = {
263278
...triggerOutput,
@@ -396,14 +411,17 @@ export class TriggerManager {
396411
name: curScf.name,
397412
triggers,
398413
});
399-
400-
createTasks.push(
401-
this.deployTrigger({
414+
const task = async () => {
415+
const res = await this.deployTrigger({
402416
name: curScf.name,
403417
namespace: curScf.namespace,
404418
events: triggersConfig,
405-
}),
406-
);
419+
});
420+
// this.runningTasks--;
421+
return res;
422+
};
423+
424+
createTasks.push(task());
407425
}
408426
const res = await Promise.all(createTasks);
409427

0 commit comments

Comments
 (0)