2024-03-25 16:05:13 +01:00
<!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 . write ( ` <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"> ` ) < / script > < 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.79.0-nightly (0824b300e 2024-03-24)" data-channel = "nightly" data-search-js = "search-42d8da7a6b9792c2.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" >
2023-12-18 17:32:00 +01:00
< 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 > − < / 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 >
2022-03-18 18:02:40 +01:00
< div class = "example-wrap" > < pre class = "language-toml" > < code > [dependencies]
2022-03-20 01:42:32 +01:00
actix-web = " 4"
2023-09-16 04:30:40 +02:00
actix-limitation = " 0.5"
2023-01-05 15:38:37 +01:00
< / code > < / pre > < / div >
2022-09-11 01:05:41 +02:00
< 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 > ;
2022-08-24 19:10:06 +02:00
< span class = "kw" > use < / span > actix_limitation::{Limiter, RateLimiter};
2022-03-18 18:02:40 +01:00
2023-12-06 15:01:24 +01:00
< span class = "attr" > #[get(< span class = "string" > "/{id}/{name}"< / span > )]
2022-08-24 19:10:06 +02:00
< / span > < span class = "kw" > async fn < / span > index(info: web::Path< (u32, String)> ) -> < span class = "kw" > impl < / span > Responder {
2023-12-06 15:01:24 +01:00
< span class = "macro" > format!< / span > (< span class = "string" > "Hello {}! id:{}"< / span > , info.< span class = "number" > 1< / span > , info.< span class = "number" > 0< / span > )
2022-03-18 18:02:40 +01:00
}
2022-11-08 10:33:31 +01:00
< span class = "attr" > #[actix_web::main]
2022-08-24 19:10:06 +02:00
< / span > < span class = "kw" > async fn < / span > main() -> std::io::Result< ()> {
< span class = "kw" > let < / span > limiter = web::Data::new(
2023-12-06 15:01:24 +01:00
Limiter::builder(< span class = "string" > "redis://127.0.0.1"< / span > )
2022-09-11 01:05:41 +02:00
.key_by(|req: < span class = "kw-2" > & < / span > ServiceRequest| {
req.get_session()
2023-12-06 15:01:24 +01:00
.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()))
2022-09-11 01:05:41 +02:00
})
2022-08-24 19:10:06 +02:00
.limit(< span class = "number" > 5000< / span > )
.period(Duration::from_secs(< span class = "number" > 3600< / span > )) < span class = "comment" > // 60 minutes
< / span > .build()
.unwrap(),
2022-03-18 18:02:40 +01:00
);
2022-08-24 19:10:06 +02:00
HttpServer::new(< span class = "kw" > move < / span > || {
App::new()
.wrap(RateLimiter::default())
.app_data(limiter.clone())
.service(index)
2022-03-18 18:02:40 +01:00
})
2023-12-06 15:01:24 +01:00
.bind((< span class = "string" > "127.0.0.1"< / span > , < span class = "number" > 8080< / span > ))< span class = "question-mark" > ?
2022-08-24 19:10:06 +02:00
< / span > .run()
.< span class = "kw" > await
< / span > }< / code > < / pre > < / div >
2024-01-22 17:07:28 +01:00
< / 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 >