ResetQuota 政策

<ph type="x-smartling-placeholder"></ph> 您正在查看 Apigee Edge 文档。
转到 Apigee X 文档
信息

内容

用于动态修改目标配额政策允许的剩余请求数。您通常可利用此政策来减少目标配额政策的当前配额计数,而不是等待配额重置。

例如,目标配额政策将开发者限制为每周 1000 个请求。在一周的第二天,开发者已达到此限制。使用“重置配额”政策从配额计数器中减去 500 个,以允许在本周剩余时间内使用其他 500 个请求。在一周结束时,配额政策会重置,开发者将恢复到一周 1000 个请求。

如需详细了解配额政策,请参阅配额政策。另请参阅这篇社区帖子,了解如何使用“重置配额”政策。

示例

以下政策代码示例说明了如何重置配额计数器:

重置默认计数器

<ResetQuota name="resetQuota">
   <Quota name="MyQuotaPolicy">
      <Identifier name="_default">
         <Allow>100</Allow>
      </Identifier>
   </Quota>
</ResetQuota>

重置配额政策使用 <Quota> 标记的 name 特性来指定目标配额政策。在上面的示例中,MyQuotaPolicy 政策是目标。

所有重置配额政策都需要使用 <Identifier> 标记来指定要更新的配额政策中的计数器。默认情况下,配额政策只有一个计数器,除非配额政策也包括 <Identifier> 标记。在此示例中,目标配额政策不使用 <Identifier> 标记,因此您可指定 name 特性作为 _default

<Allow> 元素指定用于减少目标政策的当前配额计数的值。在此示例中,配额计数减少了 100,以允许目标配额政策增加 100 个请求。当目标配额政策重置时,此更改会被丢弃。

目标配额政策的定义如下所示:

<Quota name="MyQuotaPolicy">
  <Interval>5</Interval>
  <TimeUnit>hour</TimeUnit>
  <Allow count="100"/>
</Quota>

使用引用

<ResetQuota name="resetQuota">
   <Quota ref="request.header.quotapolicy">
      <Identifier name="_default">
         <Allow ref="request.header.allowquota" />
      </Identifier>
   </Quota>
</ResetQuota>

在此示例中,您将以请求标头的形式传递目标配额政策的名称及其配额计数更改。然后,您可以在重置配额政策中引用包含这些值的流变量。

指定标识符

<ResetQuota name="resetQuota">
   <Quota name="QuotaPolicy">
      <Identifier ref="request.header.clientId">
         <Allow>100</Allow>
      </Identifier>
   </Quota>
</ResetQuota>

如果目标配额政策指定 <Identifier> 标记,则您可以为重置配额政策的 <Identifier> 标记指定相同的值,以更新特定配额计数。注意以下目标配额政策中的 <Identifier> 标记如何与为重置配额政策指定的值匹配:

<Quota name="QuotaPolicy">
  <Identifier ref="request.header.clientId"/> 
  <Interval>5</Interval>
  <TimeUnit>hour</TimeUnit>
  <Allow count="100"/>
</Quota>

元素参考

元素参考描述了重置配额政策的元素和特性。

<ResetQuota async="false" continueOnError="false" enabled="true" name="Reset-Quota-1">
   <DisplayName>Reset Quota 1</DisplayName>
   <Quota name="quotaName" ref="request.header.quotapolicy">
      <Identifier name="identifierName" ref="request.header.identifier">
         <Class ref="request.header.classIdentifier" />
         <Allow>100</Allow>
      </Identifier>
   </Quota>
</ResetQuota>

<ResetQuota> 特性

<ResetQuota async="false" continueOnError="false" enabled="true" name="Reset-Quota-1"> 

下表介绍了所有政策父元素通用的特性:

属性 说明 默认 状态
name

政策的内部名称。name 属性的值可以包含字母、数字、空格、连字符、下划线和英文句点。此值不能超过 255 个字符。

(可选)使用 <DisplayName> 元素在管理界面代理编辑器中给政策添加不同的自然语言名称标签。

不适用 必填
continueOnError

设置为 false 可在政策失败时返回错误。这是大多数政策的预期行为。

设置为 true,即使在政策失败后,仍可以继续执行流。

false 可选
enabled

设置为 true 可强制执行政策。

设为 false关闭政策。即使政策仍附加到某个流,也不会强制执行该政策。

可选
async

此特性已弃用。

false 已弃用

<DisplayName> 元素

除了用于 name 属性之外,还可以用于在管理界面代理编辑器中给政策添加不同的自然语言名称标签。

<DisplayName>Policy Display Name</DisplayName>
默认

不适用

如果省略此元素,则会使用政策的 name 属性的值。

状态 可选
类型 字符串

<Quota> 元素

标识应更新计数器的目标配额政策。

<Quota name="quotaName"  ref="request.header.quotapolicy">
   <Identifier name="identifierName" ref="request.header.identifier">
      <Allow>100</Allow>
   </Identifier>
</Quota>
默认: 不适用
状态: 必填
类型: 不适用

特性

属性 说明 默认 状态
名称

指定目标配额政策的名称。

不适用 可选
ref 包含目标配额政策名称的流变量。如果同时指定了 refname,则 ref 优先。如果 ref 在运行时没有解析,则使用 name 不适用 可选

<Quota>/<Identifier> 元素

目标配额政策指定 <Identifier> 标记时用于唯一标识计数器的变量。

<Quota name="quotaName">
   <Identifier name="identifierName" ref="request.header.identifier">
      <Allow>100</Allow>
   </Identifier>
</Quota>
默认: 不适用
状态: 必填
类型: 字符串

特性

属性 说明 默认 状态
名称

在目标配额政策中指定计数标识符的名称。对于不使用 <Identifier> 标记的配额政策,请指定 _default

不适用 可选
ref

包含目标配额政策中的计数标识符名称的流变量。如果同时指定了 refname,则 ref 优先。如果 ref 在运行时没有解析,则使用 name

不适用 可选

<Quota>/<Identifier>/<Allow> 元素

指定减少配额计数器的数量。您必须指定 <Allow>,否则政策不会修改配额。

<Identifier name="identifierName" ref="request.header.identifier">
   <Allow ref="request.header.allowquota">100</Allow>
</Identifier>
默认: 不适用
状态: 必填
类型: 整数

特性

属性 说明 默认 状态
ref

包含目标配额政策中的配额计数更改的流变量。

不适用 可选

<Quota>/<Identifier>/<Class> 元素

指定更新配额计数器的类。如需详细了解如何将类与配额政策搭配使用,请参阅配额政策

<Identifier name="_default">
   <Class ref="request.header.classIdentifier">
     <Allow>200</Allow>
   </Class>
</Identifier>
默认: 不适用
状态: 可选
类型: 不适用

特性

属性 说明 默认 状态
ref

引用包含要更新的配额类的流变量。

不适用 可选

错误参考信息

This section describes the fault codes and error messages that are returned and fault variables that are set by Edge when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.

Runtime errors

These errors can occur when the policy executes.

Fault code HTTP status Cause Fix
policies.resetquota.InvalidRLPolicy 500 The Quota policy specified in the <Quota> element of the Reset Quota policy is not defined in the API proxy and thus is not available during the flow. The <Quota> element is mandatory and identifies the target Quota policy whose counter should be updated through the Reset Quota policy.
policies.resetquota.FailedToResolveAllowCountRef N/A The reference to the variable containing the allow count in the <Allow> element of the policy cannot be resolved to a value. This element is mandatory and specifies the amount to decrease the quota counter.
policies.resetquota.FailedToResolveRLPolicy 500 The variable referenced by the ref attribute in the <Quota> element cannot be resolved.

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error name Cause Fix
InvalidCount If the count value specified in the <Allow> element of the Reset Quota Policy is not an integer, then the deployment of the API proxy fails.

架构

相关主题

配额政策