Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Upgrade the project to Actix 4.0.0 #13

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ memcached = ["r2d2-memcache", "backoff"]

[dependencies]
log = "0.4.11"
actix-web = {version = "3.3.2"}
actix-http = {version = "2.2.0", features=["actors"]}
actix = "0.10"
actix-web = {version = "4.0.0-beta.1"}
actix-http = {version = "3.0.0-beta.1", features=["actors"]}
actix = { git = "https://github.com/actix/actix" }
futures = "0.3.8"
failure = "0.1.8"

Expand All @@ -37,6 +37,6 @@ backoff = {version = "0.2.1", optional = true}
r2d2-memcache = { version = "0.5", optional = true }

[dev-dependencies]
actix-rt = "1.1.1"
actix-rt = "2.0.0-beta.2"
env_logger = "0.8.2"
version-sync = "0.9.1"
28 changes: 14 additions & 14 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use actix::dev::*;
use actix_web::{
dev::{Service, ServiceRequest, ServiceResponse, Transform},
error::Error as AWError,
error::ErrorInternalServerError,
http::{HeaderName, HeaderValue},
HttpResponse,
};
Expand Down Expand Up @@ -97,15 +98,14 @@ where
}
}

impl<T, S, B> Transform<S> for RateLimiter<T>
impl<T, S, B> Transform<S, ServiceRequest> for RateLimiter<T>
where
T: Handler<ActorMessage> + Send + Sync + 'static,
T::Context: ToEnvelope<T, ActorMessage>,
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = AWError> + 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = AWError> + 'static,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = S::Error;
type InitError = ();
Expand Down Expand Up @@ -137,15 +137,14 @@ where
identifier: Rc<Box<dyn Fn(&ServiceRequest) -> Result<String, ARError> + 'static>>,
}

impl<T, S, B> Service for RateLimitMiddleware<S, T>
impl<T, S, B> Service<ServiceRequest> for RateLimitMiddleware<S, T>
where
T: Handler<ActorMessage> + 'static,
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = AWError> + 'static,
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = AWError> + 'static,
S::Future: 'static,
B: 'static,
T::Context: ToEnvelope<T, ActorMessage>,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = S::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
Expand All @@ -162,17 +161,17 @@ where
let identifier = self.identifier.clone();
Box::pin(async move {
let identifier: String = (identifier)(&req)?;
let remaining: ActorResponse = store
let remaining: ActorResponse = match store
.send(ActorMessage::Get(String::from(&identifier)))
.await?;
.await.map_err(ErrorInternalServerError)?;
match remaining {
ActorResponse::Get(opt) => {
let opt = opt.await?;
if let Some(c) = opt {
// Existing entry in store
let expiry = store
let reset: Duration = match store
.send(ActorMessage::Expire(String::from(&identifier)))
.await?;
.await.map_err(ErrorInternalServerError)?;
let reset: Duration = match expiry {
ActorResponse::Expire(dur) => dur.await?,
_ => unreachable!(),
Expand All @@ -187,16 +186,17 @@ where
Err(response.into())
} else {
// Decrement value
let res: ActorResponse = store
let updated_value: usize = match store
.send(ActorMessage::Update {
key: identifier,
value: 1,
})
.await?;
.await.map_err(ErrorInternalServerError)?;
let updated_value: usize = match res {
ActorResponse::Update(c) => c.await?,
_ => unreachable!(),
};

// Execute the request
let fut = srv.call(req);
let mut res = fut.await?;
Expand All @@ -219,13 +219,13 @@ where
} else {
// New client, create entry in store
let current_value = max_requests - 1;
let res = store
match store
.send(ActorMessage::Set {
key: String::from(&identifier),
value: current_value,
expiry: interval,
})
.await?;
.await.map_err(ErrorInternalServerError)?;
match res {
ActorResponse::Set(c) => c.await?,
_ => unreachable!(),
Expand Down
2 changes: 1 addition & 1 deletion src/stores/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
//!
//! The above example is not thread-safe and does not implement key expiration! It's just for demonstration purposes.

#[cfg(feature = "default")]
#[cfg(feature = "memory")]
pub mod memory;

#[cfg(feature = "redis-store")]
Expand Down