1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-12-01 02:44:37 +01:00
actix-extras/actix_limitation/index.html

42 lines
8.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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/SourceSerif4-Bold-a2c9cd1067f8b328.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-deb46770fd761b69.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.75.0-nightly (e20cb7702 2023-10-12)" data-channel="nightly" data-search-js="search-8fbf244ebcf71464.js" data-settings-js="settings-74424d7eec62a23e.js" ><script src="../static.files/storage-fec3eaa3851e447d.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-5f34af1a0ee6bacd.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-5d8b3c7633ad77ba.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">&#9776;</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><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><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" title="help" tabindex="-1"><a href="../help.html">?</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-179">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">&quot;/{id}/{name}&quot;</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">&quot;Hello {}! id:{}&quot;</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">&quot;redis://127.0.0.1&quot;</span>)
.key_by(|req: <span class="kw-2">&amp;</span>ServiceRequest| {
req.get_session()
.get(<span class="kw-2">&amp;</span><span class="string">&quot;session-id&quot;</span>)
.unwrap_or_else(|<span class="kw">_</span>| req.cookie(<span class="kw-2">&amp;</span><span class="string">&quot;rate-api-id&quot;</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">&quot;127.0.0.1&quot;</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="small-section-header"><a href="#structs">Structs</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="small-section-header"><a href="#enums">Enums</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="small-section-header"><a href="#constants">Constants</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>