ResetQuota 政策

本頁內容適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

使用 ResetQuota 政策動態修改目標配額政策允許的剩餘要求數。您通常會使用這項政策來減少目標配額政策的目前配額計數,而不是等待配額計數重設。

舉例來說,目標配額政策會限制開發人員每週只能提出 1000 個要求。開發人員在當週的第二天就已達到這個上限。使用「Reset Quota」政策從配額計數器減去 500,讓使用者在一週的剩餘時間內再提出 500 個要求。每週結束時,配額政策會重設,開發人員當週可發出 1000 個要求。

這項政策是可擴充政策,使用這項政策可能會產生費用或影響用量,具體情況取決於您的 Apigee 授權。如要瞭解政策類型和使用方式的影響,請參閱「政策類型」。

如要進一步瞭解配額政策,請參閱配額政策。另請參閱這篇社群貼文,瞭解如何使用「重設配額」政策。

範例

這些政策程式碼範例說明如何重設配額計數器:

重設預設計數器

<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>

在本範例中,您會將目標配額政策的名稱和配額計數變更,以標頭形式傳遞至要求。接著,您可以在「重設配額」政策中參照含有這些值的流程變數。

指定 ID

<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> 元素,在管理 UI 代理程式編輯器中為政策加上不同、自然語言的名稱。

不適用 必填
continueOnError

將其設為 false,即可在政策失敗時傳回錯誤。這是大多數政策的預期行為。

將其設為 true,即使政策失敗,流程執行作業仍會繼續進行。另請參閱:

false 選用
enabled

設為 true 即可強制執行政策。

設為 false 即可關閉政策。即使政策仍附加至流程中,也不會強制執行。

選用
async

此屬性已淘汰。

false 已淘汰

<DisplayName> 元素

除了 name 屬性之外,您也可以在管理 UI 代理程式編輯器中使用不同的自然語言名稱標示政策。

<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>
預設值: 不適用
外觀狀態: 必填
類型: 字串

屬性

屬性 說明 預設 狀態
名稱

指定目標配額政策中的計數 ID 名稱。如果配額政策未使用 <Identifier> 標記,請指定 _default

不適用 選用
ref

流程變數,其中包含目標配額政策中的計數 ID 名稱。如果同時指定 refnameref 的優先順序較高。如果 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 Apigee 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 ResetQuota 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 ResetQuota 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 ResetQuota Policy is not an integer, then the deployment of the API proxy fails.

結構定義

相關主題

配額政策