共用方式為


模擬 Rate-Limit API 回應

Rate-Limit 標頭用於 HTTP 回應中,以限制用戶端可在指定時段內提出的要求數目。

伺服器會傳送這些標頭,以回應用戶端的要求,以指出允許多少要求,以及達到限制之前保留多少要求。

回應 RateLimit-Limit 標頭欄位會指出目前時間範圍中與客戶端相關聯的要求配額。 如果客戶端超過該限制,可能無法提供服務。

自定義速率限制支援

當您超過速率限制時,某些 API 會使用自訂行為,例如傳回 403 Forbidden 具有自定義錯誤訊息的狀態代碼。 Dev Proxy 可讓您使用 Custom 屬性的值 whenLimitExceeded 來模擬這些自定義行為。

下列範例示範如何設定如何在 devproxyrc 檔案中設定 RateLimitingPlugin,以模擬 GitHub API 的速率限制。

{
  "rateLimiting": {
    "headerLimit": "X-RateLimit-Limit",
    "headerRemaining": "X-RateLimit-Remaining",
    "headerReset": "X-RateLimit-Reset",
    "costPerRequest": 1,
    "resetTimeWindowSeconds": 3600,
    "warningThresholdPercent": 0,
    "rateLimit": 60,
    "resetFormat": "UtcEpochSeconds",
    "whenLimitExceeded": "Custom",
    "customResponseFile": "github-rate-limit-exceeded.json"
  }
}

customResponseFile包含 Proxy 在達到速率限制時所傳回的回應。

{
  "statusCode": 403,
  "headers": [
    {
      "name": "Content-Type",
      "value": "application/json; charset=utf-8"
    }
  ],
  "body": {
    "message": "You have exceeded a secondary rate limit and have been temporarily blocked from content creation. Please retry your request again later.",
    "documentation_url": "https://docs.github.com/rest/overview/resources-in-the-rest-api#secondary-rate-limits"
  }
}

下一步

深入瞭解 RateLimitingPlugin