From 2c5679f498c21e1c5a9f1d5e6e1cf7d7386c7506 Mon Sep 17 00:00:00 2001 From: dowwie Date: Mon, 16 Dec 2019 11:43:35 -0500 Subject: [PATCH 1/5] turned inner methods into async ones --- src/session.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/session.rs b/src/session.rs index 7d4fffd83..8bf0254ee 100644 --- a/src/session.rs +++ b/src/session.rs @@ -207,10 +207,10 @@ struct Inner { } impl Inner { - fn load( + async fn load( &self, req: &ServiceRequest, - ) -> impl Future, String)>, Error>> + ) -> Result, String)>, Error> { if let Ok(cookies) = req.cookies() { for cookie in cookies.iter() { @@ -220,7 +220,7 @@ impl Inner { if let Some(cookie) = jar.signed(&self.key).get(&self.name) { let value = cookie.value().to_owned(); let cachekey = (self.cache_keygen)(&cookie.value()); - return Either::Left( + return self.addr.send(Command(resp_array!["GET", cachekey])).map( |result| match result { Err(e) => Err(Error::from(e)), @@ -257,23 +257,23 @@ impl Inner { } }, }, - ), - ); + ) + .await; } else { - return Either::Right(ok(None)); + return ok(None).await } } } } - Either::Right(ok(None)) + ok(None).await } - fn update( + async fn update( &self, mut res: ServiceResponse, state: impl Iterator, value: Option, - ) -> impl Future, Error>> { + ) -> Result, Error> { let (value, jar) = if let Some(value) = value { (value.clone(), None) } else { @@ -311,8 +311,8 @@ impl Inner { let state: HashMap<_, _> = state.collect(); match serde_json::to_string(&state) { - Err(e) => Either::Left(err(e.into())), - Ok(body) => Either::Right( + Err(e) => err(e.into()).await, + Ok(body) => self.addr .send(Command(resp_array!["SET", cachekey, body, "EX", &self.ttl])) .map(|result| match result { @@ -331,13 +331,13 @@ impl Inner { } Err(err) => Err(error::ErrorInternalServerError(err)), }, - }), - ), + }) + .await, } } /// removes cache entry - fn clear_cache(&self, key: String) -> impl Future> { + async fn clear_cache(&self, key: String) -> Result<(), Error> { let cachekey = (self.cache_keygen)(&key); self.addr @@ -354,6 +354,7 @@ impl Inner { } } }) + .await } /// invalidates session cookie From bef5dfd90d8936ef3c7b1f7d00aec00e6d2d2ce6 Mon Sep 17 00:00:00 2001 From: dowwie Date: Mon, 16 Dec 2019 11:51:59 -0500 Subject: [PATCH 2/5] removed the unused import of Either --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 8bf0254ee..00730de58 100644 --- a/src/session.rs +++ b/src/session.rs @@ -10,7 +10,7 @@ use actix_web::cookie::{Cookie, CookieJar, Key, SameSite}; use actix_web::dev::{ServiceRequest, ServiceResponse}; use actix_web::http::header::{self, HeaderValue}; use actix_web::{error, Error, HttpMessage}; -use futures::future::{err, ok, Either, Future, FutureExt, Ready}; +use futures::future::{err, ok, Future, FutureExt, Ready}; use rand::{distributions::Alphanumeric, rngs::OsRng, Rng}; use redis_async::resp::RespValue; use time::{self, Duration}; From 512fe9c0e0c3e340379ccb00f5be8e670413ad8d Mon Sep 17 00:00:00 2001 From: dowwie Date: Wed, 18 Dec 2019 13:56:12 -0500 Subject: [PATCH 3/5] cleaned up obvious syntax issue --- src/session.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/session.rs b/src/session.rs index 00730de58..c4e77a0e7 100644 --- a/src/session.rs +++ b/src/session.rs @@ -260,12 +260,12 @@ impl Inner { ) .await; } else { - return ok(None).await + return Ok(None) } } } } - ok(None).await + Ok(None) } async fn update( @@ -311,7 +311,7 @@ impl Inner { let state: HashMap<_, _> = state.collect(); match serde_json::to_string(&state) { - Err(e) => err(e.into()).await, + Err(e) => Err(e.into()), Ok(body) => self.addr .send(Command(resp_array!["SET", cachekey, body, "EX", &self.ttl])) From 2d6ff41563339c1de57f15177e7cf45f03b56233 Mon Sep 17 00:00:00 2001 From: dowwie Date: Wed, 18 Dec 2019 15:07:47 -0500 Subject: [PATCH 4/5] removed map combinators --- src/session.rs | 85 ++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/src/session.rs b/src/session.rs index c4e77a0e7..240a1139b 100644 --- a/src/session.rs +++ b/src/session.rs @@ -221,44 +221,42 @@ impl Inner { let value = cookie.value().to_owned(); let cachekey = (self.cache_keygen)(&cookie.value()); return - self.addr.send(Command(resp_array!["GET", cachekey])).map( - |result| match result { - Err(e) => Err(Error::from(e)), - Ok(res) => match res { - Ok(val) => { - match val { - RespValue::Error(err) => { - return Err( - error::ErrorInternalServerError( - err, - ), - ); - } - RespValue::SimpleString(s) => { - if let Ok(val) = - serde_json::from_str(&s) - { - return Ok(Some((val, value))); - } - } - RespValue::BulkString(s) => { - if let Ok(val) = - serde_json::from_slice(&s) - { - return Ok(Some((val, value))); - } - } - _ => (), + match self.addr.send(Command(resp_array!["GET", cachekey])) + .await { + Err(e) => Err(Error::from(e)), + Ok(res) => match res { + Ok(val) => { + match val { + RespValue::Error(err) => { + return Err( + error::ErrorInternalServerError( + err, + ), + ); } - Ok(None) + RespValue::SimpleString(s) => { + if let Ok(val) = + serde_json::from_str(&s) + { + return Ok(Some((val, value))); + } + } + RespValue::BulkString(s) => { + if let Ok(val) = + serde_json::from_slice(&s) + { + return Ok(Some((val, value))); + } + } + _ => (), } - Err(err) => { - Err(error::ErrorInternalServerError(err)) - } - }, + Ok(None) + } + Err(err) => { + Err(error::ErrorInternalServerError(err)) + } }, - ) - .await; + } } else { return Ok(None) } @@ -312,10 +310,10 @@ impl Inner { let state: HashMap<_, _> = state.collect(); match serde_json::to_string(&state) { Err(e) => Err(e.into()), - Ok(body) => - self.addr + Ok(body) => { + match self.addr .send(Command(resp_array!["SET", cachekey, body, "EX", &self.ttl])) - .map(|result| match result { + .await { Err(e) => Err(Error::from(e)), Ok(redis_result) => match redis_result { Ok(_) => { @@ -331,8 +329,8 @@ impl Inner { } Err(err) => Err(error::ErrorInternalServerError(err)), }, - }) - .await, + } + } } } @@ -340,9 +338,9 @@ impl Inner { async fn clear_cache(&self, key: String) -> Result<(), Error> { let cachekey = (self.cache_keygen)(&key); - self.addr + match self.addr .send(Command(resp_array!["DEL", cachekey])) - .map(|res| match res { + .await { Err(e) => Err(Error::from(e)), Ok(res) => { match res { @@ -353,8 +351,7 @@ impl Inner { )), } } - }) - .await + } } /// invalidates session cookie From 26aa3d447a5366bfcd6face92891c22bf4ef9fe2 Mon Sep 17 00:00:00 2001 From: dowwie Date: Wed, 18 Dec 2019 15:08:08 -0500 Subject: [PATCH 5/5] removed FutureExt import --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 240a1139b..5b67f2af5 100644 --- a/src/session.rs +++ b/src/session.rs @@ -10,7 +10,7 @@ use actix_web::cookie::{Cookie, CookieJar, Key, SameSite}; use actix_web::dev::{ServiceRequest, ServiceResponse}; use actix_web::http::header::{self, HeaderValue}; use actix_web::{error, Error, HttpMessage}; -use futures::future::{err, ok, Future, FutureExt, Ready}; +use futures::future::{err, ok, Future, Ready}; use rand::{distributions::Alphanumeric, rngs::OsRng, Rng}; use redis_async::resp::RespValue; use time::{self, Duration};