Description
Implementation of the Governor rate limiter for Rocket web framework.
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 or 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 providing HTTP headers in the response.
Usage
Add dependencies to rocket-governor and rocket-governor-derive crates to your Cargo.toml.
Implement RocketGovernable
for a guard struct which derives from RocketGovernor
:
use ;
use ;
;
This requires to implement the 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.
If your struct has members, there is an alternative derive
which requires to implement Default
:
There is a small helper function nonzero(u32)
for creating Quotas in your quota()
-implementation e.g.:
per_second
After this you can add your Guard to your route-methods like:
Register Catcher
To handle HTTP Status 421 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 ratelimitguard_rocket_governor_catcher()
and ratelimitguardwithmember_rocket_governor_catcher()
.
Register it with register(<path>, <catchers>)
-method of Rocket:
Additional information
To understand the basics of Rocket, please have a look in the Rocket Guide:
- https://rocket.rs/v0.5-rc/guide/requests/#request-guards
- https://rocket.rs/v0.5-rc/guide/requests/#error-catchers
Licenses
You can choose between MIT License or Apache License 2.0.
MIT License
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
http://www.apache.org/licenses/LICENSE-2.0
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.