mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 23:51:06 +01:00
41 lines
8.2 KiB
HTML
41 lines
8.2 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-b21aa549bf6d91ff.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.80.0-nightly (9c9b56879 2024-05-05)" data-channel="nightly" data-search-js="search-d52510db62a78183.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-e32f0c247825364d.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-09095024cf37855e.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"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../actix_limitation/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Type ‘S’ or ‘/’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings">Settings</a></div></form></nav><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>−</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 = "4"
|
||
actix-limitation = "0.5"
|
||
</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<(u32, String)>) -> <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() -> std::io::Result<()> {
|
||
<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">&</span>ServiceRequest| {
|
||
req.get_session()
|
||
.get(<span class="kw-2">&</span><span class="string">"session-id"</span>)
|
||
.unwrap_or_else(|<span class="kw">_</span>| req.cookie(<span class="kw-2">&</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> |