您正在查看 Apigee Edge 說明文件。
查看 Apigee X 說明文件。 資訊
優勢
使用以動態方式修改目標配額政策允許的剩餘要求數量。您通常會透過這項政策減少目標配額政策目前的配額數量,而不必等待配額重設作業。
舉例來說,目標配額政策限制開發人員每週最多只能提出 1000 個要求。直到當週的第二天,開發人員已達到此上限。您可以使用重設配額政策,將配額計數器從配額計數器減去 500,在當週的剩餘時間內可再提出 500 個要求。本週結束時,配額政策會重設,而開發人員會在當週返回 1,000 個要求。
如要進一步瞭解配額政策,請參閱配額政策。另請參閱這篇社群文章,瞭解如何使用重設配額政策。
範例
下列政策程式碼範例說明如何重設配額計數器:
重設預設計數器
<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 |
政策的內部名稱。 您也可以選擇使用 |
不適用 | 需要 |
continueOnError |
如果設為 設為 |
false | 選用 |
enabled |
如要強制執行政策,請設為 設為 |
true | 選用 |
async |
此屬性已淘汰。 |
false | 已淘汰 |
<DisplayName> 元素
除了 name
屬性外,您還可以使用不同的自然語言名稱,在管理 UI Proxy 編輯器中為政策加上標籤。
<DisplayName>Policy Display Name</DisplayName>
預設 |
不適用 如果省略這個元素,系統會使用政策的 |
---|---|
存在必要性 | 選用 |
類型 | 字串 |
<Quota> 元素
找出要更新計數器的目標配額政策。
<Quota name="quotaName" ref="request.header.quotapolicy"> <Identifier name="identifierName" ref="request.header.identifier"> <Allow>100</Allow> </Identifier> </Quota>
預設: | 不適用 |
所在地: | 需要 |
類型: | 不適用 |
屬性
屬性 | 說明 | 預設 | 存在必要性 |
---|---|---|---|
名稱 |
指定目標配額政策的名稱。 |
不適用 | 選用 |
參考資料 | 包含目標配額政策名稱的流程變數。如果同時指定 ref 和 name,ref 會優先處理。如果 ref 未在執行階段解析,系統會使用 name。 | 不適用 | 選用 |
<Quota>/<Identifier> 元素
當目標配額政策指定 <Identifier> 標記時,用來識別計數器的變數。
<Quota name="quotaName"> <Identifier name="identifierName" ref="request.header.identifier"> <Allow>100</Allow> </Identifier> </Quota>
預設: | 不適用 |
所在地: | 需要 |
類型: | 字串 |
屬性
屬性 | 說明 | 預設 | 存在必要性 |
---|---|---|---|
名稱 |
在目標配額政策中指定計數 ID 名稱。如為未使用 <Identifier> 標記的配額政策,請指定 _default。 |
不適用 | 選用 |
參考資料 |
包含目標配額政策中計數 ID 名稱的流程變數。如果同時指定 ref 和 name,ref 會優先採用。如果 ref 未在執行階段解析,系統會使用 name。 |
不適用 | 選用 |
<Quota>/<Identifier>/<Allow> 元素
指定減少配額計數器的金額。您必須指定 <Allow>,政策不會修改配額。
<Identifier name="identifierName" ref="request.header.identifier"> <Allow ref="request.header.allowquota">100</Allow> </Identifier>
預設: | 不適用 |
所在地: | 需要 |
類型: | 整數 |
屬性
屬性 | 說明 | 預設 | 存在必要性 |
---|---|---|---|
參考資料 |
流量變數,其中包含目標配額政策中的配額數量變更。 |
不適用 | 選用 |
<Quota>/<Identifier>/<Class> 元素
指定配額計數器的更新類別。如要進一步瞭解如何搭配配額政策使用類別,請參閱配額政策。
<Identifier name="_default"> <Class ref="request.header.classIdentifier"> <Allow>200</Allow> </Class> </Identifier>
預設: | 不適用 |
所在地: | 選用 |
類型: | 不適用 |
屬性
屬性 | 說明 | 預設 | 存在必要性 |
---|---|---|---|
參考資料 |
參照包含要更新的配額類別的資料流變數。 |
不適用 | 選用 |
錯誤參考資料
本節說明當這項政策觸發錯誤時,傳回的錯誤代碼和錯誤訊息,以及 Edge 設定的錯誤變數。 如果您正在開發錯誤規則來處理錯誤,請務必瞭解這項資訊。詳情請參閱「政策錯誤須知」和「處理錯誤」。
執行階段錯誤
執行政策時,可能會發生這些錯誤。
錯誤代碼 | HTTP 狀態 | 原因 | 修正 |
---|---|---|---|
policies.resetquota.InvalidRLPolicy |
500 | 重設配額政策的 <Quota> 元素中指定的配額政策未在 API Proxy 中定義,因此不適用於流程。<Quota> 是必要元素,用於識別應透過「重設配額」政策更新的目標配額政策。 |
build |
policies.resetquota.FailedToResolveAllowCountRef |
不適用 | 如果參照的變數含有政策 <Allow> 元素中的允許計數,就無法將參照的變數解析為值。此為必要元素,並指定配額計數器的金額。 |
build |
policies.resetquota.FailedToResolveRLPolicy |
500 | 無法解析 <Quota> 元素中 ref 屬性所參照的變數。 |
build |
部署錯誤
若您部署包含這項政策的 Proxy,就可能會發生這些錯誤。
錯誤名稱 | 原因 | 修正 |
---|---|---|
InvalidCount |
如果重設配額政策的 <Allow> 元素中指定的計數值不是整數,API Proxy 部署就會失敗。 |
build |