1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 08:22:43 +01:00
actix-extras/actix_limitation/index.html

43 lines
8.6 KiB
HTML
Raw Normal View History

<!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><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-e935ef01ae1c1829.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.78.0-nightly (516b6162a 2024-03-03)" data-channel="nightly" data-search-js="search-dd67cee4cfa65049.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-4c98445ec4002617.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-12cf3b4f4f9dc36d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-04d5337699b92874.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="Click or press S 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"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></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"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="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>