模拟 Rate-Limit API 响应
Rate-Limit 标头用于 HTTP 响应,以限制客户端在给定时间段内可以发出的请求数。
服务器发送这些标头以响应客户端的请求,以指示允许的请求数以及达到限制之前剩余的请求数。
响应 RateLimit-Limit
标头字段指示当前时间范围内与客户端关联的请求配额。 如果客户端超过该限制,可能不会提供服务。
自定义速率限制支持
超过速率限制时,某些 API 会使用自定义行为,例如返回 403 Forbidden
带有自定义错误消息的状态代码。 开发代理允许你通过使用 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
包含当应用达到速率限制时代理返回的响应。
{
"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
。