mirror of
https://github.com/actix/actix-extras.git
synced 2025-09-03 05:19:23 +02:00
Deploying to gh-pages from @ 32313c0af6
🚀
This commit is contained in:
@@ -159,6 +159,29 @@
|
||||
<span id="157">157</span>
|
||||
<span id="158">158</span>
|
||||
<span id="159">159</span>
|
||||
<span id="160">160</span>
|
||||
<span id="161">161</span>
|
||||
<span id="162">162</span>
|
||||
<span id="163">163</span>
|
||||
<span id="164">164</span>
|
||||
<span id="165">165</span>
|
||||
<span id="166">166</span>
|
||||
<span id="167">167</span>
|
||||
<span id="168">168</span>
|
||||
<span id="169">169</span>
|
||||
<span id="170">170</span>
|
||||
<span id="171">171</span>
|
||||
<span id="172">172</span>
|
||||
<span id="173">173</span>
|
||||
<span id="174">174</span>
|
||||
<span id="175">175</span>
|
||||
<span id="176">176</span>
|
||||
<span id="177">177</span>
|
||||
<span id="178">178</span>
|
||||
<span id="179">179</span>
|
||||
<span id="180">180</span>
|
||||
<span id="181">181</span>
|
||||
<span id="182">182</span>
|
||||
</pre><pre class="rust"><code><span class="doccomment">//! Rate limiter using a fixed window counter for arbitrary keys, backed by Redis for Actix Web.
|
||||
//!
|
||||
//! ```toml
|
||||
@@ -168,8 +191,9 @@
|
||||
</span><span class="doccomment">//! ```
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use std::time::Duration;
|
||||
//! use actix_web::{get, web, App, HttpServer, Responder};
|
||||
//! use std::{sync::Arc, time::Duration};
|
||||
//! use actix_web::{dev::ServiceRequest, get, web, App, HttpServer, Responder};
|
||||
//! use actix_session::SessionExt as _;
|
||||
//! use actix_limitation::{Limiter, RateLimiter};
|
||||
//!
|
||||
//! #[get("/{id}/{name}")]
|
||||
@@ -181,8 +205,11 @@
|
||||
//! async fn main() -> std::io::Result<()> {
|
||||
//! let limiter = web::Data::new(
|
||||
//! Limiter::builder("redis://127.0.0.1")
|
||||
//! .cookie_name("session-id".to_owned())
|
||||
//! .session_key("rate-api-id".to_owned())
|
||||
//! .key_by(|req: &ServiceRequest| {
|
||||
//! req.get_session()
|
||||
//! .get(&"session-id")
|
||||
//! .unwrap_or_else(|_| req.cookie(&"rate-api-id").map(|c| c.to_string()))
|
||||
//! })
|
||||
//! .limit(5000)
|
||||
//! .period(Duration::from_secs(3600)) // 60 minutes
|
||||
//! .build()
|
||||
@@ -207,8 +234,9 @@
|
||||
#![doc(html_logo_url = <span class="string">"https://actix.rs/img/logo.png"</span>)]
|
||||
#![doc(html_favicon_url = <span class="string">"https://actix.rs/favicon.ico"</span>)]
|
||||
|
||||
</span><span class="kw">use </span>std::{borrow::Cow, time::Duration};
|
||||
</span><span class="kw">use </span>std::{borrow::Cow, fmt, sync::Arc, time::Duration};
|
||||
|
||||
<span class="kw">use </span>actix_web::dev::ServiceRequest;
|
||||
<span class="kw">use </span>redis::Client;
|
||||
|
||||
<span class="kw">mod </span>builder;
|
||||
@@ -231,16 +259,34 @@
|
||||
</span><span class="kw">pub const </span>DEFAULT_COOKIE_NAME: <span class="kw-2">&</span>str = <span class="string">"sid"</span>;
|
||||
|
||||
<span class="doccomment">/// Default session key.
|
||||
</span><span class="attribute">#[cfg(feature = <span class="string">"session"</span>)]
|
||||
</span><span class="kw">pub const </span>DEFAULT_SESSION_KEY: <span class="kw-2">&</span>str = <span class="string">"rate-api-id"</span>;
|
||||
|
||||
<span class="doccomment">/// Helper trait to impl Debug on GetKeyFn type
|
||||
</span><span class="kw">trait </span>GetKeyFnT: Fn(<span class="kw-2">&</span>ServiceRequest) -> <span class="prelude-ty">Option</span><String> {}
|
||||
|
||||
<span class="kw">impl</span><T> GetKeyFnT <span class="kw">for </span>T <span class="kw">where </span>T: Fn(<span class="kw-2">&</span>ServiceRequest) -> <span class="prelude-ty">Option</span><String> {}
|
||||
|
||||
<span class="doccomment">/// Get key function type with auto traits
|
||||
</span><span class="kw">type </span>GetKeyFn = <span class="kw">dyn </span>GetKeyFnT + Send + Sync;
|
||||
|
||||
<span class="doccomment">/// Get key resolver function type
|
||||
</span><span class="kw">impl </span>fmt::Debug <span class="kw">for </span>GetKeyFn {
|
||||
<span class="kw">fn </span>fmt(<span class="kw-2">&</span><span class="self">self</span>, f: <span class="kw-2">&mut </span>fmt::Formatter<<span class="lifetime">'_</span>>) -> fmt::Result {
|
||||
<span class="macro">write!</span>(f, <span class="string">"GetKeyFn"</span>)
|
||||
}
|
||||
}
|
||||
|
||||
<span class="doccomment">/// Wrapped Get key function Trait
|
||||
</span><span class="kw">type </span>GetArcBoxKeyFn = Arc<GetKeyFn>;
|
||||
|
||||
<span class="doccomment">/// Rate limiter.
|
||||
</span><span class="attribute">#[derive(Debug, Clone)]
|
||||
</span><span class="kw">pub struct </span>Limiter {
|
||||
client: Client,
|
||||
limit: usize,
|
||||
period: Duration,
|
||||
cookie_name: Cow<<span class="lifetime">'static</span>, str>,
|
||||
session_key: Cow<<span class="lifetime">'static</span>, str>,
|
||||
get_key_fn: GetArcBoxKeyFn,
|
||||
}
|
||||
|
||||
<span class="kw">impl </span>Limiter {
|
||||
@@ -254,8 +300,10 @@
|
||||
redis_url: redis_url.into(),
|
||||
limit: DEFAULT_REQUEST_LIMIT,
|
||||
period: Duration::from_secs(DEFAULT_PERIOD_SECS),
|
||||
get_key_fn: <span class="prelude-val">None</span>,
|
||||
cookie_name: Cow::Borrowed(DEFAULT_COOKIE_NAME),
|
||||
session_key: Cow::Borrowed(DEFAULT_SESSION_KEY),
|
||||
<span class="attribute">#[cfg(feature = <span class="string">"session"</span>)]
|
||||
</span>session_key: Cow::Borrowed(DEFAULT_SESSION_KEY),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -307,16 +355,14 @@
|
||||
|
||||
<span class="attribute">#[test]
|
||||
</span><span class="kw">fn </span>test_create_limiter() {
|
||||
<span class="kw">let </span>builder = Limiter::builder(<span class="string">"redis://127.0.0.1:6379/1"</span>);
|
||||
<span class="kw">let </span><span class="kw-2">mut </span>builder = Limiter::builder(<span class="string">"redis://127.0.0.1:6379/1"</span>);
|
||||
<span class="kw">let </span>limiter = builder.build();
|
||||
<span class="macro">assert!</span>(limiter.is_ok());
|
||||
|
||||
<span class="kw">let </span>limiter = limiter.unwrap();
|
||||
<span class="macro">assert_eq!</span>(limiter.limit, <span class="number">5000</span>);
|
||||
<span class="macro">assert_eq!</span>(limiter.period, Duration::from_secs(<span class="number">3600</span>));
|
||||
<span class="macro">assert_eq!</span>(limiter.cookie_name, DEFAULT_COOKIE_NAME);
|
||||
<span class="macro">assert_eq!</span>(limiter.session_key, DEFAULT_SESSION_KEY);
|
||||
}
|
||||
}
|
||||
</code></pre></div>
|
||||
</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="actix_limitation" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0-nightly (eaadb8947 2022-08-27)" ></div></body></html>
|
||||
</section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="actix_limitation" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.65.0-nightly (1d37ed661 2022-09-09)" ></div></body></html>
|
Reference in New Issue
Block a user