1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

address clippy warnings (#134)

This commit is contained in:
Rob Ede 2020-11-23 10:37:56 +00:00 committed by GitHub
parent ab80a91468
commit 99e98f1a29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 71 deletions

View File

@ -37,8 +37,10 @@ impl RedisActor {
pub fn start<S: Into<String>>(addr: S) -> Addr<RedisActor> { pub fn start<S: Into<String>>(addr: S) -> Addr<RedisActor> {
let addr = addr.into(); let addr = addr.into();
let mut backoff = ExponentialBackoff::default(); let backoff = ExponentialBackoff {
backoff.max_elapsed_time = None; max_elapsed_time: None,
..Default::default()
};
Supervisor::start(|_| RedisActor { Supervisor::start(|_| RedisActor {
addr, addr,

View File

@ -221,51 +221,56 @@ impl Inner {
&self, &self,
req: &ServiceRequest, req: &ServiceRequest,
) -> Result<Option<(HashMap<String, String>, String)>, Error> { ) -> Result<Option<(HashMap<String, String>, String)>, Error> {
if let Ok(cookies) = req.cookies() { // wrapped in block to avoid holding `Ref` (from `req.cookies`) across await point
for cookie in cookies.iter() { let (value, cache_key) = {
if cookie.name() == self.name { let cookies = if let Ok(cookies) = req.cookies() {
let mut jar = CookieJar::new(); cookies
jar.add_original(cookie.clone()); } else {
if let Some(cookie) = jar.signed(&self.key).get(&self.name) { return Ok(None);
let value = cookie.value().to_owned(); };
let cachekey = (self.cache_keygen)(&cookie.value());
return match self if let Some(cookie) =
.addr cookies.iter().find(|&cookie| cookie.name() == self.name)
.send(Command(resp_array!["GET", cachekey])) {
.await let mut jar = CookieJar::new();
{ jar.add_original(cookie.clone());
Err(e) => Err(Error::from(e)),
Ok(res) => match res { if let Some(cookie) = jar.signed(&self.key).get(&self.name) {
Ok(val) => { let value = cookie.value().to_owned();
match val { let cache_key = (self.cache_keygen)(&cookie.value());
RespValue::Error(err) => { (value, cache_key)
return Err( } else {
error::ErrorInternalServerError(err), return Ok(None);
); }
} } else {
RespValue::SimpleString(s) => { return Ok(None);
if let Ok(val) = serde_json::from_str(&s) { }
return Ok(Some((val, value))); };
}
} let res = self
RespValue::BulkString(s) => { .addr
if let Ok(val) = serde_json::from_slice(&s) { .send(Command(resp_array!["GET", cache_key]))
return Ok(Some((val, value))); .await?;
}
} let val = res.map_err(error::ErrorInternalServerError)?;
_ => (),
} match val {
Ok(None) RespValue::Error(err) => {
} return Err(error::ErrorInternalServerError(err));
Err(err) => Err(error::ErrorInternalServerError(err)), }
}, RespValue::SimpleString(s) => {
}; if let Ok(val) = serde_json::from_str(&s) {
} else { return Ok(Some((val, value)));
return Ok(None);
}
} }
} }
RespValue::BulkString(s) => {
if let Ok(val) = serde_json::from_slice(&s) {
return Ok(Some((val, value)));
}
}
_ => {}
} }
Ok(None) Ok(None)
} }
@ -308,41 +313,37 @@ impl Inner {
(value, Some(jar)) (value, Some(jar))
}; };
let cachekey = (self.cache_keygen)(&value); let cache_key = (self.cache_keygen)(&value);
let state: HashMap<_, _> = state.collect(); let state: HashMap<_, _> = state.collect();
match serde_json::to_string(&state) {
Err(e) => Err(e.into()), let body = match serde_json::to_string(&state) {
Ok(body) => { Err(e) => return Err(e.into()),
match self Ok(body) => body,
.addr };
.send(Command(resp_array!["SET", cachekey, body, "EX", &self.ttl]))
.await let cmd = Command(resp_array!["SET", cache_key, body, "EX", &self.ttl]);
{
Err(e) => Err(Error::from(e)), self.addr
Ok(redis_result) => match redis_result { .send(cmd)
Ok(_) => { .await?
if let Some(jar) = jar { .map_err(error::ErrorInternalServerError)?;
for cookie in jar.delta() {
let val = if let Some(jar) = jar {
HeaderValue::from_str(&cookie.to_string())?; for cookie in jar.delta() {
res.headers_mut().append(header::SET_COOKIE, val); let val = HeaderValue::from_str(&cookie.to_string())?;
} res.headers_mut().append(header::SET_COOKIE, val);
}
Ok(res)
}
Err(err) => Err(error::ErrorInternalServerError(err)),
},
}
} }
} }
Ok(res)
} }
/// removes cache entry /// removes cache entry
async fn clear_cache(&self, key: String) -> Result<(), Error> { async fn clear_cache(&self, key: String) -> Result<(), Error> {
let cachekey = (self.cache_keygen)(&key); let cache_key = (self.cache_keygen)(&key);
match self.addr.send(Command(resp_array!["DEL", cachekey])).await { match self.addr.send(Command(resp_array!["DEL", cache_key])).await {
Err(e) => Err(Error::from(e)), Err(e) => Err(Error::from(e)),
Ok(res) => { Ok(res) => {
match res { match res {
@ -445,7 +446,7 @@ mod test {
} }
#[actix_rt::test] #[actix_rt::test]
async fn test_workflow() { async fn test_session_workflow() {
// Step 1: GET index // Step 1: GET index
// - set-cookie actix-session will be in response (session cookie #1) // - set-cookie actix-session will be in response (session cookie #1)
// - response should be: {"counter": 0, "user_id": None} // - response should be: {"counter": 0, "user_id": None}