Rate limiting is used to control the rate requests are received and handled by an endpoint of the web application or web service.
Rocket specific features
Define as many rate limits with Quota of Governor
as you like and need in your Rocket web application/service.
It is implemented as a Rocket Request Guard and provides also an implementation of an Error Catcher.
The Error Catcher can be registered on any path to handle
Status::TooManyRequests and provide HTTP headers in the response.
RocketGovernable for a guard struct which derives from
use ; use ; ;
This requires to implement the method
fn quota(_: Method, _: &str) -> Quota.
You can vary your
Quota on any combination of method and route_name, but the returned
Quota should be a static-like. It should not change between invocations of the
quota()-method with equal parameters.
If your struct has members, there is an alternative
derive which requires to implement
There is a small helper function
nonzero(u32) for creating Quotas in your
After implementing the minimal requirements of the derived struct you can add your Guard to your route-methods like:
To handle HTTP Status 429 TooManyRequests there is an catcher-function implementation.
It is called like your
struct-name in lowercase with the extension _rocket_governor_catcher.
So in this usage example there exist two catcher-functions
Register it with
register(<path>, <catchers>)-method of Rocket:
To understand the basics of Rocket, please visit the Rocket Guide:
Copyright (c) 2021 Markus Kolb
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Apache License 2.0
Copyright 2021 Markus Kolb
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.