Cloud Endpoints 提供配額,讓您控管應用程式呼叫您 API 的頻率。您可以設定配額來指定使用量限制,藉此保護 API 免於收到呼叫應用程式的過量要求。要求過多可能是因為簡單的錯字,或是系統設計不當,導致不必要地呼叫 API。無論原因為何,一旦來源流量達到特定程度,就必須封鎖,才能確保 API 整體運作正常。設定配額可確保一個應用程式不會對使用您 API 的其他應用程式造成負面影響。
本頁面提供與配額相關的重要功能總覽。
要求與用戶專案相連結
設定配額後,Endpoints 即會追蹤每個用戶的 Google Cloud 專案在每分鐘提出要求的次數。每個呼叫 API 的應用程式都必須符合以下條件:
隨著每項要求將 API 金鑰如此一來,Endpoints 就能識別與呼叫應用程式相關聯的Google Cloud 專案,並增加該 Google Cloud 專案的要求計數器計次。
您可以讓 API 消費者在Google Cloud 控制台中建立自己的專案,也可以為他們建立專案。由於 Endpoints 會以「專案」為單位執行配額限制,因此您必須為每個 API 用戶準備一項專案。
限制每分鐘提出的要求次數
您可以透過設定配額,限制每分鐘對整個 API 提出的要求次數;或者只限特定方法的要求次數。如果來自用戶專案的用戶端程式碼超過您設定的限制,則要求會在存取您的 API 之前遭拒,而系統則會傳回 HTTP 狀態碼 429 too many
requests。呼叫應用程式必須處理 429 狀態碼的問題,並使用指數輪詢或其他重試邏輯,以降低對 API 的呼叫頻率。
設定一或多個配額
您可以設定一或多個已命名的配額,並為每個配額指定不同的頻率限制。例如,您的 API 可能具備某些會大量消耗資源的方法 (例如執行複雜查詢並傳回大型結果清單的方法),也可能包含其他較為快速且輕量的方法。因此,您可能會想要設定兩個頻率限制相異的配額,將大量消耗資源的方法與其中一個配額建立關聯,並將輕量方法和另一個配額建立關聯。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eCloud Endpoints quotas control the rate at which applications call an API, protecting it from excessive requests and ensuring one application doesn't negatively impact others.\u003c/p\u003e\n"],["\u003cp\u003eQuotas are tracked per minute per consumer Google Cloud project, requiring each application to have a project, enable the API, and send an API key.\u003c/p\u003e\n"],["\u003cp\u003eQuotas limit the number of requests per minute to an entire API or specific methods, rejecting requests exceeding the limit with a \u003ccode\u003e429 too many requests\u003c/code\u003e HTTP status code.\u003c/p\u003e\n"],["\u003cp\u003eMultiple quotas with different rate limits can be configured to accommodate resource-intensive and lightweight methods, also you can configure a request cost for each method as an alternative.\u003c/p\u003e\n"],["\u003cp\u003eThe enforced quota limit has an error margin of 30%, because the proxy uses aggregation and batching to improve request latency.\u003c/p\u003e\n"]]],[],null,["# About quotas\n\n[OpenAPI](/endpoints/docs/openapi/quotas-overview \"View this page for the Cloud Endpoints OpenAPI docs\") \\| gRPC\n\n\u003cbr /\u003e\n\n|\n| **Beta**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nCloud Endpoints provides quotas, which let you control the rate at which\napplications can call your API. Setting a quota lets you specify usage\nlimits to protect your API from an excessive number of requests from\ncalling applications. The excessive requests might have been caused\nby a simple typo or from an inefficiently designed system that makes needless\ncalls to your API. Regardless of the cause, blocking traffic from a\nsource once it reaches a certain level is necessary for the overall health of\nyour API. By setting a quota, you ensure that one application cannot negatively\nimpact other applications that use your API.\n\nThis page provides an overview of the key functionality provided by quotas.\n\nRequests are tied to the consumer project\n-----------------------------------------\n\nAfter you configure a quota, Endpoints tracks the number of\nrequests per minute per consumer Google Cloud project. Each\napplication that calls your API must:\n\n- Have a Google Cloud project.\n- Have [enabled your API](/endpoints/docs/grpc/control-api-callers) in their Google Cloud project.\n- Send an [API key](/endpoints/docs/grpc/restricting-api-access-with-api-keys) with each request to your API. This lets Endpoints identify the Google Cloud project that the calling application is associated with and to increment the request counter for the Google Cloud project.\n\nYou can either have your API consumers create their own projects in the\nGoogle Cloud console, or you can create the projects for them. Because\nEndpoints enforces quotas per *project*, you must have one\nproject for each API consumer.\n\nLimit the number of requests per minute\n---------------------------------------\n\nBy setting a quota, you can limit the number of requests per minute to your\nentire API or only to specific methods. If the client code from a consumer\nproject exceeds the limit that you have configured, the request is rejected\nbefore it gets to your API, and an HTTP status code of [`429 too many\nrequests`](https://tools.ietf.org/html/rfc6585#section-4) is returned. Calling\napplications will need to handle the `429` status code and use [exponential\nbackoff](https://wikipedia.org/wiki/Exponential_backoff) or some other retry\nlogic to decrease the rate of calls to your API.\n| **The enforced limit has 30% error margin.** Endpoints uses aggregation and batching at the proxy to reduce the runtime request latency. Instead of calling quota server to check available quota for each request, the proxy aggregates and batches the quota calls. As a result, the enforced quota limit is approximate, with a 30% margin of error. For example, if the expected limit is 100 requests per minute, the quota limit enforced could range from 70 requests per minutes to 130 requests per minute.\n\nConfigure one or more quotas\n----------------------------\n\nYou can configure one or more named quotas and specify a different rate limit\nfor each quota. For example, you could have some methods in your API that are\nresource-intensive (such as a method that runs a complex query and returns a\nlarge list of results), and other methods that are fast and lightweight. You\nmight want to configure two quotas with different rate limits, and associate the\nresource-intensive methods with one quota, and the lightweight methods with the\nother quota.\n\nConfigure a cost\n----------------\n\nWhen you associate a method with a quota, you always specify a cost for the\nrequest. This allows different methods to consume the same quota at different\nrates. You can use costs as an alternative to configuring different quotas.\nFor example, suppose you configure a quota with a limit of 1000 requests per\nminute. For the lightweight methods, you configure a cost of 1, which means\nclients can call the lightweight methods 1000 times per minute. For the\nresource-intensive methods, you configure a cost of 2, which means that each\ntime the client calls the method, the request counter is incremented by 2, until\nthe limit of 1000 is reached. In effect, this limits the resource-intensive\nmethods to 500 requests per minute.\n\nOverride the configured quota\n-----------------------------\n\nThe **Endpoints** \\\u003e **Services** page displays the quota configured for each\nmethod in your API. If needed, you can override the configured limit for a\nspecific consumer project. To set an override, you need to enter the project\nnumber of the consumer project on the **Endpoints** \\\u003e **Services** page. If you\ndon't have access to the consumer project that you want to override, you need to\ncontact someone who has access to obtain the project number.\n\nWhat's next\n-----------\n\n- [Configuring quotas](/endpoints/docs/grpc/quotas-configure)\n- [Overriding quotas](/endpoints/docs/grpc/quotas-override)"]]