mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 17:22:57 +01:00
address clippy warnings (#134)
This commit is contained in:
parent
ab80a91468
commit
99e98f1a29
@ -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,
|
||||||
|
@ -221,27 +221,42 @@ 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() {
|
||||||
|
cookies
|
||||||
|
} else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(cookie) =
|
||||||
|
cookies.iter().find(|&cookie| cookie.name() == self.name)
|
||||||
|
{
|
||||||
let mut jar = CookieJar::new();
|
let mut jar = CookieJar::new();
|
||||||
jar.add_original(cookie.clone());
|
jar.add_original(cookie.clone());
|
||||||
|
|
||||||
if let Some(cookie) = jar.signed(&self.key).get(&self.name) {
|
if let Some(cookie) = jar.signed(&self.key).get(&self.name) {
|
||||||
let value = cookie.value().to_owned();
|
let value = cookie.value().to_owned();
|
||||||
let cachekey = (self.cache_keygen)(&cookie.value());
|
let cache_key = (self.cache_keygen)(&cookie.value());
|
||||||
return match self
|
(value, cache_key)
|
||||||
|
} else {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = self
|
||||||
.addr
|
.addr
|
||||||
.send(Command(resp_array!["GET", cachekey]))
|
.send(Command(resp_array!["GET", cache_key]))
|
||||||
.await
|
.await?;
|
||||||
{
|
|
||||||
Err(e) => Err(Error::from(e)),
|
let val = res.map_err(error::ErrorInternalServerError)?;
|
||||||
Ok(res) => match res {
|
|
||||||
Ok(val) => {
|
|
||||||
match val {
|
match val {
|
||||||
RespValue::Error(err) => {
|
RespValue::Error(err) => {
|
||||||
return Err(
|
return Err(error::ErrorInternalServerError(err));
|
||||||
error::ErrorInternalServerError(err),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
RespValue::SimpleString(s) => {
|
RespValue::SimpleString(s) => {
|
||||||
if let Ok(val) = serde_json::from_str(&s) {
|
if let Ok(val) = serde_json::from_str(&s) {
|
||||||
@ -253,19 +268,9 @@ impl Inner {
|
|||||||
return Ok(Some((val, value)));
|
return Ok(Some((val, value)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => {}
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
Err(err) => Err(error::ErrorInternalServerError(err)),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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?
|
||||||
|
.map_err(error::ErrorInternalServerError)?;
|
||||||
|
|
||||||
if let Some(jar) = jar {
|
if let Some(jar) = jar {
|
||||||
for cookie in jar.delta() {
|
for cookie in jar.delta() {
|
||||||
let val =
|
let val = HeaderValue::from_str(&cookie.to_string())?;
|
||||||
HeaderValue::from_str(&cookie.to_string())?;
|
|
||||||
res.headers_mut().append(header::SET_COOKIE, val);
|
res.headers_mut().append(header::SET_COOKIE, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
Err(err) => Err(error::ErrorInternalServerError(err)),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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}
|
||||||
|
Loading…
Reference in New Issue
Block a user