以下是一些HTTP API限速HTTP响应头的示例,取自四个常见的REST API:Github、Vimeo、Twitter和Imgur:
#=============================#=============================================#
# HTTP Header # Description #
#=============================#=============================================#
| X-RateLimit-Limit | Request limit per hour |
+-----------------------------+---------------------------------------------+
| X-RateLimit-Remaining | The number of requests left for the time |
| | window |
+-----------------------------+---------------------------------------------+
#=============================#=============================================#
# HTTP Header # Description #
#=============================#=============================================#
| X-RateLimit-Limit | Request limit per day / per 5 minutes |
+-----------------------------+---------------------------------------------+
| X-RateLimit-Remaining | The number of requests left for the time |
| | window |
+-----------------------------+---------------------------------------------+
| X-RateLimit-Reset | The remaining window before the rate limit |
| | resets in UTC epoch seconds |
+-----------------------------+---------------------------------------------+
注意: Twitter 使用类似于 Vimeo 的标题,但是每个名称中都有另一个破折号。
#=============================#=============================================#
# HTTP Header # Description #
#=============================#=============================================#
| X-Rate-Limit-Limit | The rate limit ceiling for that given |
| | request |
+-----------------------------+---------------------------------------------+
| X-Rate-Limit-Remaining | The number of requests left for the |
| | 15 minute window |
+-----------------------------+---------------------------------------------+
| X-Rate-Limit-Reset | The remaining window before the rate limit |
| | resets in UTC epoch seconds |
+-----------------------------+---------------------------------------------+
#=============================#=============================================#
# HTTP Header # Description #
#=============================#=============================================#
| X-RateLimit-UserLimit | Total credits that can be allocated |
+-----------------------------+---------------------------------------------+
| X-RateLimit-UserRemaining | Total credits available |
+-----------------------------+---------------------------------------------+
| X-RateLimit-UserReset | Timestamp (unix epoch) for when the credits |
| | will be reset |
+-----------------------------+---------------------------------------------+
| X-RateLimit-ClientLimit | Total credits that can be allocated for the |
| | application in a day |
+-----------------------------+---------------------------------------------+
| X-RateLimit-ClientRemaining | Total credits remaining for the application |
| | in a day |
+-----------------------------+---------------------------------------------+
除了API特定的标头之外,不要忘记使用普通的标准的Retry-After
标头
服务器发送“Retry-After”头字段以指示用户代理在发起后续请求之前应等待多长时间...... 该字段的值可以是HTTP日期或接收到响应后延迟的秒数。
标准对于将其与503或3xx状态代码一起使用时提出了具体的附加建议:
当使用503(服务不可用)响应发送时,Retry-After表示服务预计将向客户端不可用的时间。 当使用任何3xx(重定向)响应发送时,Retry-After表示要求用户代理在发出重定向请求之前等待的最短时间。
Retry-After
旨在与 503
或 30x
响应一起使用。https://tools.ietf.org/html/rfc7231#section-7.1.3 - Russbear
response.headers["x-ratelimit-limit"]
- Manish