Skip to content

Commit d7ee0d3

Browse files
authored
feat: add sync request concurrent option (#258)
1 parent 56a7b81 commit d7ee0d3

File tree

6 files changed

+39
-24
lines changed

6 files changed

+39
-24
lines changed

apps/cli/src/command/helper.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class BaseCommand extends Command {
3333
}
3434

3535
// Appends the provided examples to description
36-
public addExamples(examples: Array<{ title: string, command: string }>) {
36+
public addExamples(examples: Array<{ title: string; command: string }>) {
3737
this.examples.push(...examples);
3838

3939
// Title of each example is a comment which describes the actual command
@@ -105,17 +105,15 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
105105
.default('apisix'),
106106
)
107107
.addOption(
108-
new Option(
109-
'--server <string>',
110-
'HTTP address of the backend',
111-
)
108+
new Option('--server <string>', 'HTTP address of the backend')
112109
.env('ADC_SERVER')
113110
.default('http://localhost:9180'),
114111
)
115112
.addOption(
116-
new Option('--token <string>', 'token for ADC to connect to the backend').env(
117-
'ADC_TOKEN',
118-
),
113+
new Option(
114+
'--token <string>',
115+
'token for ADC to connect to the backend',
116+
).env('ADC_TOKEN'),
119117
)
120118
.addOption(
121119
new Option(
@@ -212,3 +210,14 @@ export class BackendCommand<OPTS extends object = object> extends BaseCommand {
212210
}
213211

214212
export const NoLintOption = new Option('--no-lint', 'disable lint check');
213+
export const RequestConcurrentOption = new Option(
214+
'--request-concurrent <integer>',
215+
'number of concurrent requests to the backend',
216+
)
217+
.default(10, '10')
218+
.argParser((val) => {
219+
const int = parseInt(val);
220+
if (!Number.isInteger(int))
221+
throw new InvalidArgumentError('Not an integer');
222+
return int;
223+
});

apps/cli/src/command/ingress-sync.command.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,16 @@ import {
1212
} from '../tasks';
1313
import { InitializeBackendTask } from '../tasks/init_backend';
1414
import { TaskContext } from './diff.command';
15-
import { BackendCommand } from './helper';
16-
import { BackendOptions } from './typing';
17-
18-
type SyncOption = BackendOptions & {
19-
file: Array<string>;
20-
lint: boolean;
21-
22-
// experimental feature
23-
remoteStateFile: string;
24-
};
15+
import { BackendCommand, RequestConcurrentOption } from './helper';
16+
import { SyncOption } from './sync.command';
2517

2618
export const IngressSyncCommand = new BackendCommand<SyncOption>('sync')
2719
.option(
2820
'-f, --file <file-path>',
2921
'file to synchronize',
3022
(filePath, files: Array<string> = []) => files.concat(filePath),
3123
)
24+
.addOption(RequestConcurrentOption)
3225
.handle(async (opts) => {
3326
const tasks = new Listr<TaskContext, typeof SilentRenderer>(
3427
[
@@ -54,7 +47,10 @@ export const IngressSyncCommand = new BackendCommand<SyncOption>('sync')
5447
try {
5548
const results = await lastValueFrom(
5649
ctx.backend
57-
.sync(ctx.diff, { exitOnFailure: false })
50+
.sync(ctx.diff, {
51+
exitOnFailure: false,
52+
concurrent: opts.requestConcurrent,
53+
})
5854
.pipe(toArray()),
5955
);
6056

apps/cli/src/command/sync.command.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ import {
1111
import { InitializeBackendTask } from '../tasks/init_backend';
1212
import { SignaleRenderer } from '../utils/listr';
1313
import { TaskContext } from './diff.command';
14-
import { BackendCommand, NoLintOption } from './helper';
14+
import {
15+
BackendCommand,
16+
NoLintOption,
17+
RequestConcurrentOption,
18+
} from './helper';
1519
import { BackendOptions } from './typing';
1620
import { addBackendEventListener } from './utils';
1721

18-
type SyncOption = BackendOptions & {
22+
export type SyncOption = BackendOptions & {
1923
file: Array<string>;
2024
lint: boolean;
25+
requestConcurrent?: number;
2126

2227
// experimental feature
2328
remoteStateFile: string;
@@ -34,6 +39,7 @@ export const SyncCommand = new BackendCommand<SyncOption>(
3439
(filePath, files: Array<string> = []) => files.concat(filePath),
3540
)
3641
.addOption(NoLintOption)
42+
.addOption(RequestConcurrentOption)
3743
.addExamples([
3844
{
3945
title: 'Synchronize configuration from a single file',
@@ -95,7 +101,10 @@ export const SyncCommand = new BackendCommand<SyncOption>(
95101
const cancel = addBackendEventListener(ctx.backend, task);
96102
await lastValueFrom(
97103
ctx.backend
98-
.sync(ctx.diff, { exitOnFailure: true })
104+
.sync(ctx.diff, {
105+
exitOnFailure: true,
106+
concurrent: opts.requestConcurrent,
107+
})
99108
.pipe(toArray()),
100109
);
101110
cancel();

libs/backend-api7/src/operator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class Operator extends ADCSDK.backend.BackendEventSource {
102102
}),
103103
tap(() => logger(taskStateEvent('TASK_DONE'))),
104104
);
105-
}),
105+
}, opts.concurrent),
106106
),
107107
),
108108
);

libs/backend-apisix/src/operator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class Operator extends ADCSDK.backend.BackendEventSource {
123123
}),
124124
tap(() => logger(taskStateEvent('TASK_DONE'))),
125125
);
126-
}),
126+
}, opts.concurrent),
127127
),
128128
),
129129
);

libs/sdk/src/backend/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface BackendEventAxiosDebug {
4949
}
5050

5151
export interface BackendSyncOptions {
52+
concurrent?: number;
5253
exitOnFailure?: boolean;
5354
}
5455

0 commit comments

Comments
 (0)