1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00
actix-extras/actix_limitation/index.html

41 lines
7.6 KiB
HTML

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Rate limiter using a fixed window counter for arbitrary keys, backed by Redis for Actix Web."><title>actix_limitation - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-dd39b87e5fcfba68.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="actix_limitation" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0-nightly (f21554f7f 2024-06-08)" data-channel="nightly" data-search-js="search-9d4fa2dd5e5a7e17.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-20a3ad099b048cf2.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../actix_limitation/index.html"><img src="https://actix.rs/img/logo.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../actix_limitation/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2><a href="../actix_limitation/index.html">actix_limitation</a><span class="version">0.5.1</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li></ul></section></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">actix_limitation</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../src/actix_limitation/lib.rs.html#1-180">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Rate limiter using a fixed window counter for arbitrary keys, backed by Redis for Actix Web.</p>
<div class="example-wrap"><pre class="language-toml"><code>[dependencies]
actix-web = &quot;4&quot;
actix-limitation = &quot;0.5&quot;
</code></pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::{sync::Arc, time::Duration};
<span class="kw">use </span>actix_web::{dev::ServiceRequest, get, web, App, HttpServer, Responder};
<span class="kw">use </span>actix_session::SessionExt <span class="kw">as _</span>;
<span class="kw">use </span>actix_limitation::{Limiter, RateLimiter};
<span class="attr">#[get(<span class="string">"/{id}/{name}"</span>)]
</span><span class="kw">async fn </span>index(info: web::Path&lt;(u32, String)&gt;) -&gt; <span class="kw">impl </span>Responder {
<span class="macro">format!</span>(<span class="string">"Hello {}! id:{}"</span>, info.<span class="number">1</span>, info.<span class="number">0</span>)
}
<span class="attr">#[actix_web::main]
</span><span class="kw">async fn </span>main() -&gt; std::io::Result&lt;()&gt; {
<span class="kw">let </span>limiter = web::Data::new(
Limiter::builder(<span class="string">"redis://127.0.0.1"</span>)
.key_by(|req: <span class="kw-2">&amp;</span>ServiceRequest| {
req.get_session()
.get(<span class="kw-2">&amp;</span><span class="string">"session-id"</span>)
.unwrap_or_else(|<span class="kw">_</span>| req.cookie(<span class="kw-2">&amp;</span><span class="string">"rate-api-id"</span>).map(|c| c.to_string()))
})
.limit(<span class="number">5000</span>)
.period(Duration::from_secs(<span class="number">3600</span>)) <span class="comment">// 60 minutes
</span>.build()
.unwrap(),
);
HttpServer::new(<span class="kw">move </span>|| {
App::new()
.wrap(RateLimiter::default())
.app_data(limiter.clone())
.service(index)
})
.bind((<span class="string">"127.0.0.1"</span>, <span class="number">8080</span>))<span class="question-mark">?
</span>.run()
.<span class="kw">await
</span>}</code></pre></div>
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Builder.html" title="struct actix_limitation::Builder">Builder</a></div><div class="desc docblock-short">Rate limiter builder.</div></li><li><div class="item-name"><a class="struct" href="struct.Limiter.html" title="struct actix_limitation::Limiter">Limiter</a></div><div class="desc docblock-short">Rate limiter.</div></li><li><div class="item-name"><a class="struct" href="struct.RateLimiter.html" title="struct actix_limitation::RateLimiter">RateLimiter</a></div><div class="desc docblock-short">Rate limit middleware.</div></li><li><div class="item-name"><a class="struct" href="struct.Status.html" title="struct actix_limitation::Status">Status</a></div><div class="desc docblock-short">A report for a given key containing the limit status.</div></li></ul><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="enum" href="enum.Error.html" title="enum actix_limitation::Error">Error</a></div><div class="desc docblock-short">Failure modes of the rate limiter.</div></li></ul><h2 id="constants" class="section-header">Constants<a href="#constants" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="constant" href="constant.DEFAULT_COOKIE_NAME.html" title="constant actix_limitation::DEFAULT_COOKIE_NAME">DEFAULT_COOKIE_NAME</a></div><div class="desc docblock-short">Default cookie name.</div></li><li><div class="item-name"><a class="constant" href="constant.DEFAULT_PERIOD_SECS.html" title="constant actix_limitation::DEFAULT_PERIOD_SECS">DEFAULT_PERIOD_SECS</a></div><div class="desc docblock-short">Default period (in seconds).</div></li><li><div class="item-name"><a class="constant" href="constant.DEFAULT_REQUEST_LIMIT.html" title="constant actix_limitation::DEFAULT_REQUEST_LIMIT">DEFAULT_REQUEST_LIMIT</a></div><div class="desc docblock-short">Default request limit.</div></li><li><div class="item-name"><a class="constant" href="constant.DEFAULT_SESSION_KEY.html" title="constant actix_limitation::DEFAULT_SESSION_KEY">DEFAULT_SESSION_KEY</a></div><div class="desc docblock-short">Default session key.</div></li></ul></section></div></main></body></html>