1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-27 17:22:57 +01:00

remove Limiter builder lifetime

This commit is contained in:
Rob Ede 2022-07-11 02:05:22 +01:00
parent 169b262c66
commit d5dc087e93
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 13 additions and 11 deletions

View File

@ -1,7 +1,9 @@
# Changes # Changes
## Unreleased - 2022-xx-xx ## Unreleased - 2022-xx-xx
- Updated `session-session` dependency to `0.7`. - `Limiter::builder` now takes an `impl Into<String>`.
- Removed lifetime from `Builder`.
- Updated `actix-session` dependency to `0.7`.
## 0.2.0 - 2022-03-22 ## 0.2.0 - 2022-03-22

View File

@ -4,17 +4,17 @@ use redis::Client;
use crate::{errors::Error, Limiter}; use crate::{errors::Error, Limiter};
/// Rate limit builder. /// Rate limiter builder.
#[derive(Debug)] #[derive(Debug)]
pub struct Builder<'a> { pub struct Builder {
pub(crate) redis_url: &'a str, pub(crate) redis_url: String,
pub(crate) limit: usize, pub(crate) limit: usize,
pub(crate) period: Duration, pub(crate) period: Duration,
pub(crate) cookie_name: Cow<'static, str>, pub(crate) cookie_name: Cow<'static, str>,
pub(crate) session_key: Cow<'static, str>, pub(crate) session_key: Cow<'static, str>,
} }
impl Builder<'_> { impl Builder {
/// Set upper limit. /// Set upper limit.
pub fn limit(&mut self, limit: usize) -> &mut Self { pub fn limit(&mut self, limit: usize) -> &mut Self {
self.limit = limit; self.limit = limit;
@ -45,7 +45,7 @@ impl Builder<'_> {
/// **synchronous** operation. /// **synchronous** operation.
pub fn build(&self) -> Result<Limiter, Error> { pub fn build(&self) -> Result<Limiter, Error> {
Ok(Limiter { Ok(Limiter {
client: Client::open(self.redis_url)?, client: Client::open(self.redis_url.as_str())?,
limit: self.limit, limit: self.limit,
period: self.period, period: self.period,
cookie_name: self.cookie_name.clone(), cookie_name: self.cookie_name.clone(),
@ -63,7 +63,7 @@ mod tests {
let redis_url = "redis://127.0.0.1"; let redis_url = "redis://127.0.0.1";
let period = Duration::from_secs(10); let period = Duration::from_secs(10);
let builder = Builder { let builder = Builder {
redis_url, redis_url: redis_url.to_owned(),
limit: 100, limit: 100,
period, period,
cookie_name: Cow::Owned("session".to_string()), cookie_name: Cow::Owned("session".to_string()),
@ -82,7 +82,7 @@ mod tests {
let redis_url = "redis://127.0.0.1"; let redis_url = "redis://127.0.0.1";
let period = Duration::from_secs(20); let period = Duration::from_secs(20);
let mut builder = Builder { let mut builder = Builder {
redis_url, redis_url: redis_url.to_owned(),
limit: 100, limit: 100,
period: Duration::from_secs(10), period: Duration::from_secs(10),
session_key: Cow::Borrowed("key"), session_key: Cow::Borrowed("key"),
@ -109,7 +109,7 @@ mod tests {
let redis_url = "127.0.0.1"; let redis_url = "127.0.0.1";
let period = Duration::from_secs(20); let period = Duration::from_secs(20);
let mut builder = Builder { let mut builder = Builder {
redis_url, redis_url: redis_url.to_owned(),
limit: 100, limit: 100,
period: Duration::from_secs(10), period: Duration::from_secs(10),
session_key: Cow::Borrowed("key"), session_key: Cow::Borrowed("key"),

View File

@ -88,9 +88,9 @@ impl Limiter {
/// See [`redis-rs` docs](https://docs.rs/redis/0.21/redis/#connection-parameters) on connection /// See [`redis-rs` docs](https://docs.rs/redis/0.21/redis/#connection-parameters) on connection
/// parameters for how to set the Redis URL. /// parameters for how to set the Redis URL.
#[must_use] #[must_use]
pub fn builder(redis_url: &str) -> Builder<'_> { pub fn builder(redis_url: impl Into<String>) -> Builder {
Builder { Builder {
redis_url, redis_url: redis_url.into(),
limit: DEFAULT_REQUEST_LIMIT, limit: DEFAULT_REQUEST_LIMIT,
period: Duration::from_secs(DEFAULT_PERIOD_SECS), period: Duration::from_secs(DEFAULT_PERIOD_SECS),
cookie_name: Cow::Borrowed(DEFAULT_COOKIE_NAME), cookie_name: Cow::Borrowed(DEFAULT_COOKIE_NAME),