From e70dd6456f5046e9e2837f3804df3d924269bf30 Mon Sep 17 00:00:00 2001 From: wtifs Date: Thu, 31 Aug 2023 21:00:29 +0800 Subject: [PATCH 1/3] feat: add support for cpu limiter --- .../accessLimiting/detail/Page.tsx | 29 +++++++- .../accessLimiting/detail/PageDuck.ts | 3 +- .../administration/accessLimiting/model.ts | 6 +- .../accessLimiting/operations/Create.tsx | 73 ++++++++++++++++--- .../accessLimiting/operations/CreateDuck.ts | 2 + .../administration/accessLimiting/types.ts | 49 ++++++++++++- 6 files changed, 143 insertions(+), 19 deletions(-) diff --git a/web/src/polaris/administration/accessLimiting/detail/Page.tsx b/web/src/polaris/administration/accessLimiting/detail/Page.tsx index 613028e8..5857e184 100644 --- a/web/src/polaris/administration/accessLimiting/detail/Page.tsx +++ b/web/src/polaris/administration/accessLimiting/detail/Page.tsx @@ -5,7 +5,16 @@ import DetailPage from '@src/polaris/common/duckComponents/DetailPage' import { Values } from '../operations/CreateDuck' import { Form, Card, Text, Table, H6, FormItem, FormText } from 'tea-component' -import { LimitType, LimitTypeMap, LimitMethodTypeMap, LimitAction, LimitActionMap, LimitFailoverMap } from '../types' +import { + LimitResource, + LimitResourceMap, + LimitType, + LimitTypeMap, + LimitMethodTypeMap, + LimitAction, + LimitActionMap, + LimitFailoverMap, MaxAmountUnit, MaxAmountHeader, +} from '../types' import insertCSS from '@src/polaris/common/helpers/insertCSS' insertCSS( @@ -38,6 +47,7 @@ export default purify(function AccessLimitingDetailPag(props: DuckCmpProps {name || '-'} + + + {LimitResourceMap[strResource]} + + {LimitTypeMap[strLimitType]} @@ -183,12 +198,20 @@ export default purify(function AccessLimitingDetailPag(props: DuckCmpProps { + const { precision } = item + return {precision} + }, + }, { key: 'maxAmount', - header: '请求数阈值', + header: MaxAmountHeader[strResource], render: item => { const { maxAmount } = item - return {maxAmount}次 + return {maxAmount}{MaxAmountUnit[strResource]} }, }, ]} diff --git a/web/src/polaris/administration/accessLimiting/detail/PageDuck.ts b/web/src/polaris/administration/accessLimiting/detail/PageDuck.ts index aba91c7e..47bdaa4e 100644 --- a/web/src/polaris/administration/accessLimiting/detail/PageDuck.ts +++ b/web/src/polaris/administration/accessLimiting/detail/PageDuck.ts @@ -112,7 +112,7 @@ export default class AccessLimitingDetailPageDuck extends DetailPage { result.totalCount > 0 && result.list.map((item: RateLimit) => ({ ...item, - disable: item.disable === false ? true : false, + disable: item.disable, amounts: item.amounts?.length > 0 ? item.amounts.map(o => ({ @@ -120,6 +120,7 @@ export default class AccessLimitingDetailPageDuck extends DetailPage { maxAmount: o.maxAmount, validDurationNum: Number(o.validDuration.substring(0, o.validDuration.length - 1)), validDurationUnit: o.validDuration.substring(o.validDuration.length - 1, o.validDuration.length), + precision: o.precision, })) : [], arguments: diff --git a/web/src/polaris/administration/accessLimiting/model.ts b/web/src/polaris/administration/accessLimiting/model.ts index 94770d03..452ec662 100644 --- a/web/src/polaris/administration/accessLimiting/model.ts +++ b/web/src/polaris/administration/accessLimiting/model.ts @@ -6,7 +6,7 @@ import { LimitMethodType, LimitArgumentsType, LimitFailover, - LimitAmountsValidationUnit, + LimitAmountsValidationUnit, LimitResource, } from './types' export interface DescribeLimitRulesParams { @@ -26,6 +26,7 @@ export interface DescribeLimitRulesParams { export interface LimitConfig { validDuration: string maxAmount: number + precision: number } export interface LimitConfigForFormFilling { @@ -34,6 +35,7 @@ export interface LimitConfigForFormFilling { maxAmount: number validDurationNum: number validDurationUnit: LimitAmountsValidationUnit + precision: number // 滑动窗口精度 } interface LimitMethodConfig { @@ -101,6 +103,7 @@ export async function describeLimitRules(params: DescribeLimitRulesParams) { export interface CreateLimitRulesBaseParams { id?: string name: string // 规则名 + resource: LimitResource // 限流资源 type: LimitType // 限流类型 namespace: string // 规则所属命名空间 service: string // 规则所属服务名 @@ -110,7 +113,6 @@ export interface CreateLimitRulesBaseParams { max_queue_delay: number // 匀速排队的最大排队时长 failover: LimitFailover // 失败处理策略 disable: boolean // 是否停用该限流规则,默认启用 - resource: string } export interface CreateLimitRulesParams extends CreateLimitRulesBaseParams { diff --git a/web/src/polaris/administration/accessLimiting/operations/Create.tsx b/web/src/polaris/administration/accessLimiting/operations/Create.tsx index ea47fdbb..1ba75a8c 100644 --- a/web/src/polaris/administration/accessLimiting/operations/Create.tsx +++ b/web/src/polaris/administration/accessLimiting/operations/Create.tsx @@ -22,6 +22,8 @@ import Input from '@src/polaris/common/duckComponents/form/Input' import InputNumber from '@src/polaris/common/duckComponents/form/InputNumber' import Switch from '@src/polaris/common/duckComponents/form/Switch' import { + LimitResource, + LimitResourceOptions, LimitTypeOptions, LimitType, LimitMethodTypeOptions, @@ -33,7 +35,7 @@ import { LimitFailoverOptions, LimitFailover, LimitAmountsValidationUnit, - LimitAmountsValidationUnitOptions, + LimitAmountsValidationUnitOptions, MaxAmountUnit, MaxAmountHeader, } from '../types' import insertCSS from '@src/polaris/common/helpers/insertCSS' import { FieldAPI } from '@src/polaris/common/ducks/Form' @@ -76,6 +78,7 @@ export default purify(function LimitRuleCreatePage(props: DuckCmpProps o.value === LimitType.LOCAL) + + // 只有QPS单机限流模式才有匀速排队 + let limitActionOptions = LimitActionOptions const limitType = typeField.getValue() - const limitActionOptions = - limitType === LimitType.LOCAL ? LimitActionOptions : LimitActionOptions.filter(o => o.value === LimitAction.REJECT) + if (limitResource === LimitResource.QPS) { + if (limitType === LimitType.LOCAL) { + // 单机QPS限流 展示 快速失败 + 匀速排队 + limitActionOptions = LimitActionOptions.filter(o => o.value !== LimitAction.BBR) + } else { + // 分布式QPS限流 只展示 快速失败 + limitActionOptions = LimitActionOptions.filter(o => o.value === LimitAction.REJECT) + } + } else { + limitActionOptions = LimitActionOptions.filter(o => o.value === LimitAction.BBR) + } + // 获取arguments amounts列表值 const argumentsList = argumentsField.getValue() const amountsList = amountsField.getValue() @@ -217,7 +237,8 @@ export default purify(function LimitRuleCreatePage(props: DuckCmpProps + + + { + resourceField.setValue(LimitResource[value]) + if (value === LimitResource.CPU) { + typeField.setValue(LimitType.LOCAL) + actionField.setValue(LimitAction.BBR) + } + }} + /> + + { typeField.setValue(LimitType[value]) if (value === LimitType.GLOBAL) { @@ -458,7 +494,7 @@ export default purify(function LimitRuleCreatePage(props: DuckCmpProps { + const { precision } = item.getFields(['precision']) + return ( + + precision.setValue(+value)} + hideButton + size='l' + /> + + ) + }, + }, { key: 'maxAmount', - header: '请求数阈值', + header: MaxAmountHeader[limitResource], render: item => { const { maxAmount } = item.getFields(['maxAmount']) return ( - + diff --git a/web/src/polaris/administration/accessLimiting/operations/CreateDuck.ts b/web/src/polaris/administration/accessLimiting/operations/CreateDuck.ts index ee3cc3cd..30c2e4a2 100644 --- a/web/src/polaris/administration/accessLimiting/operations/CreateDuck.ts +++ b/web/src/polaris/administration/accessLimiting/operations/CreateDuck.ts @@ -221,6 +221,7 @@ export default class LimitRuleCreatePageDuck extends DetailPage { const handledAmounts = values.amounts.map(item => ({ maxAmount: item.maxAmount, validDuration: `${item.validDurationNum}${item.validDurationUnit}`, + precision: item.precision, })) const handledArguments = values.arguments.map(item => ({ @@ -278,6 +279,7 @@ export default class LimitRuleCreatePageDuck extends DetailPage { maxAmount: o.maxAmount, validDurationNum: Number(o.validDuration.substring(0, o.validDuration.length - 1)), validDurationUnit: o.validDuration.substring(o.validDuration.length - 1, o.validDuration.length), + precision: o.precision, })) : [], arguments: diff --git a/web/src/polaris/administration/accessLimiting/types.ts b/web/src/polaris/administration/accessLimiting/types.ts index 3e245109..0d11b42c 100644 --- a/web/src/polaris/administration/accessLimiting/types.ts +++ b/web/src/polaris/administration/accessLimiting/types.ts @@ -5,6 +5,41 @@ export interface Lists { serviceList: [] } +// 限制资源类型,支持QPS, CPU +export enum LimitResource { + QPS = 'QPS', + // CONCURRENCY = 'CONCURRENCY', // 并发量 + CPU = 'CPU', +} +export const LimitResourceOptions = [ + { + value: LimitResource.QPS, + text: 'QPS', + }, + { + value: LimitResource.CPU, + text: 'CPU使用率', + }, +] +export const LimitResourceMap = LimitResourceOptions.reduce((map, curr) => { + map[curr.value] = curr.text + return map +}, {}) + +// 阈值的标题 +export enum MaxAmountHeader { + QPS = '请求数阈值', + // CONCURRENCY = '次', + CPU = 'CPU使用率阈值', +} + +// 阈值的计数单位 +export enum MaxAmountUnit { + QPS = '次', + // CONCURRENCY = '次', + CPU = '%', +} + // 限流类型,支持LOCAL(单机限流), GLOBAL(分布式限流) export enum LimitType { GLOBAL = 'GLOBAL', @@ -26,10 +61,11 @@ export const LimitTypeMap = LimitTypeOptions.reduce((map, curr) => { return map }, {}) -// 限流效果,支持REJECT(直接拒绝),UNIRATE(匀速排队),默认REJECT +// 限流效果,支持REJECT(直接拒绝),UNIRATE(匀速排队),BBR(自适应),默认REJECT export enum LimitAction { - REJECT = 'REJECT', - UNIRATE = 'UNIRATE', + REJECT = 'REJECT', // 实现为令牌桶 + UNIRATE = 'UNIRATE', // 实现为漏桶 + BBR = 'BBR', } export const LimitActionOptions = [ { @@ -40,6 +76,10 @@ export const LimitActionOptions = [ value: LimitAction.UNIRATE, text: '匀速排队', }, + { + value: LimitAction.BBR, + text: '自适应', + }, ] export const LimitActionMap = LimitActionOptions.reduce((map, curr) => { @@ -213,6 +253,7 @@ export const generateDefaultValues: Values = { validDurationNum: 1, validDurationUnit: LimitAmountsValidationUnit.s, maxAmount: 1, + precision: 1, }, ], regex_combine: true, @@ -220,5 +261,5 @@ export const generateDefaultValues: Values = { max_queue_delay: 1, failover: LimitFailover.FAILOVER_LOCAL, disable: true, - resource: 'QPS', + resource: LimitResource.QPS, } From 041cc0a0c991b97618106049b452419b09154ac5 Mon Sep 17 00:00:00 2001 From: wtifs Date: Mon, 4 Sep 2023 13:53:22 +0800 Subject: [PATCH 2/3] adjust default precision --- .../administration/accessLimiting/detail/Page.tsx | 7 ++++++- .../administration/accessLimiting/operations/Create.tsx | 6 +++--- web/src/polaris/administration/accessLimiting/types.ts | 9 ++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/web/src/polaris/administration/accessLimiting/detail/Page.tsx b/web/src/polaris/administration/accessLimiting/detail/Page.tsx index 5857e184..0e231d0a 100644 --- a/web/src/polaris/administration/accessLimiting/detail/Page.tsx +++ b/web/src/polaris/administration/accessLimiting/detail/Page.tsx @@ -211,7 +211,12 @@ export default purify(function AccessLimitingDetailPag(props: DuckCmpProps { const { maxAmount } = item - return {maxAmount}{MaxAmountUnit[strResource]} + return ( + + {maxAmount} + {MaxAmountUnit[strResource]} + + ) }, }, ]} diff --git a/web/src/polaris/administration/accessLimiting/operations/Create.tsx b/web/src/polaris/administration/accessLimiting/operations/Create.tsx index 1ba75a8c..e624f85a 100644 --- a/web/src/polaris/administration/accessLimiting/operations/Create.tsx +++ b/web/src/polaris/administration/accessLimiting/operations/Create.tsx @@ -35,7 +35,7 @@ import { LimitFailoverOptions, LimitFailover, LimitAmountsValidationUnit, - LimitAmountsValidationUnitOptions, MaxAmountUnit, MaxAmountHeader, + LimitAmountsValidationUnitOptions, MaxAmountUnit, MaxAmountHeader, MaxAmountDefault, } from '../types' import insertCSS from '@src/polaris/common/helpers/insertCSS' import { FieldAPI } from '@src/polaris/common/ducks/Form' @@ -599,10 +599,10 @@ export default purify(function LimitRuleCreatePage(props: DuckCmpProps amountsField.asArray().push({ id: `${Math.round(Math.random() * 10000)}`, - maxAmount: 1, + maxAmount: MaxAmountDefault[limitResource], validDurationNum: 1, validDurationUnit: LimitAmountsValidationUnit.s, - precision: 1, + precision: 10, }) } > diff --git a/web/src/polaris/administration/accessLimiting/types.ts b/web/src/polaris/administration/accessLimiting/types.ts index 0d11b42c..514eae18 100644 --- a/web/src/polaris/administration/accessLimiting/types.ts +++ b/web/src/polaris/administration/accessLimiting/types.ts @@ -40,6 +40,13 @@ export enum MaxAmountUnit { CPU = '%', } +// 阈值的计数单位 +export enum MaxAmountDefault { + QPS = 1, + // CONCURRENCY = '次', + CPU = 80, +} + // 限流类型,支持LOCAL(单机限流), GLOBAL(分布式限流) export enum LimitType { GLOBAL = 'GLOBAL', @@ -253,7 +260,7 @@ export const generateDefaultValues: Values = { validDurationNum: 1, validDurationUnit: LimitAmountsValidationUnit.s, maxAmount: 1, - precision: 1, + precision: 10, }, ], regex_combine: true, From 76cf3c4cf4b5db279a28f5cc438a63b92c0c58c1 Mon Sep 17 00:00:00 2001 From: wtifs Date: Mon, 4 Sep 2023 16:31:24 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E9=A1=B5=E5=90=AF=E7=94=A8=E5=AD=97=E6=AE=B5=E5=8E=9F=E6=9C=89?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../polaris/administration/accessLimiting/detail/PageDuck.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/src/polaris/administration/accessLimiting/detail/PageDuck.ts b/web/src/polaris/administration/accessLimiting/detail/PageDuck.ts index 47bdaa4e..c763f167 100644 --- a/web/src/polaris/administration/accessLimiting/detail/PageDuck.ts +++ b/web/src/polaris/administration/accessLimiting/detail/PageDuck.ts @@ -112,7 +112,7 @@ export default class AccessLimitingDetailPageDuck extends DetailPage { result.totalCount > 0 && result.list.map((item: RateLimit) => ({ ...item, - disable: item.disable, + disable: item.disable === false ? true : false, amounts: item.amounts?.length > 0 ? item.amounts.map(o => ({