1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-27 10:39:03 +02:00

upgrade to actix/actix-web 0.7

This commit is contained in:
Nikolay Kim
2018-07-20 14:53:56 -07:00
parent 93707884d2
commit 62c382d280
6 changed files with 57 additions and 66 deletions

View File

@ -5,13 +5,14 @@
//! * [API Documentation (Releases)](https://docs.rs/actix-redis/)
//! * [Chat on gitter](https://gitter.im/actix/actix)
//! * Cargo package: [actix-redis](https://crates.io/crates/actix-redis)
//! * Minimum supported Rust version: 1.21 or later
//! * Minimum supported Rust version: 1.26 or later
//!
extern crate actix;
extern crate backoff;
extern crate futures;
extern crate tokio_core;
extern crate tokio_codec;
extern crate tokio_io;
extern crate tokio_tcp;
#[macro_use]
extern crate log;
#[macro_use]
@ -53,9 +54,6 @@ pub enum Error {
Disconnected,
}
unsafe impl Send for Error {}
unsafe impl Sync for Error {}
impl From<redis_async::error::Error> for Error {
fn from(err: redis_async::error::Error) -> Error {
Error::Redis(err)

View File

@ -1,7 +1,7 @@
use std::collections::VecDeque;
use std::io;
use actix::actors::{Connect, Connector};
use actix::actors::resolver::{Connect, Resolver};
use actix::prelude::*;
use backoff::backoff::Backoff;
use backoff::ExponentialBackoff;
@ -9,10 +9,10 @@ use futures::unsync::oneshot;
use futures::Future;
use redis_async::error::Error as RespError;
use redis_async::resp::{RespCodec, RespValue};
use tokio_core::net::TcpStream;
use tokio_io::codec::FramedRead;
use tokio_codec::FramedRead;
use tokio_io::io::WriteHalf;
use tokio_io::AsyncRead;
use tokio_tcp::TcpStream;
use Error;
@ -34,11 +34,11 @@ pub struct RedisActor {
impl RedisActor {
/// Start new `Supervisor` with `RedisActor`.
pub fn start<S: Into<String>>(addr: S) -> Addr<Unsync, RedisActor> {
pub fn start<S: Into<String>>(addr: S) -> Addr<RedisActor> {
let addr = addr.into();
Supervisor::start(|_| RedisActor {
addr: addr,
addr,
cell: None,
backoff: ExponentialBackoff::default(),
queue: VecDeque::new(),
@ -50,7 +50,7 @@ impl Actor for RedisActor {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Context<Self>) {
Connector::from_registry()
Resolver::from_registry()
.send(Connect::host(self.addr.as_str()))
.into_actor(self)
.map(|res, act, ctx| match res {
@ -104,10 +104,7 @@ impl Supervised for RedisActor {
impl actix::io::WriteHandler<io::Error> for RedisActor {
fn error(&mut self, err: io::Error, _: &mut Self::Context) -> Running {
warn!(
"Redis connection dropped: {} error: {}",
self.addr, err
);
warn!("Redis connection dropped: {} error: {}", self.addr, err);
Running::Stop
}
}
@ -139,9 +136,6 @@ impl Handler<Command> for RedisActor {
let _ = tx.send(Err(Error::NotConnected));
}
Box::new(
rx.map_err(|_| Error::Disconnected)
.and_then(|res| res),
)
Box::new(rx.map_err(|_| Error::Disconnected).and_then(|res| res))
}
}

View File

@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::iter::FromIterator;
use std::iter;
use std::rc::Rc;
use actix::prelude::*;
@ -10,6 +10,7 @@ use cookie::{Cookie, CookieJar, Key};
use futures::future::{err as FutErr, ok as FutOk, Either};
use futures::Future;
use http::header::{self, HeaderValue};
use rand::distributions::Alphanumeric;
use rand::{self, Rng};
use redis_async::resp::RespValue;
use serde_json;
@ -105,15 +106,15 @@ impl<S> SessionBackend<S> for RedisSessionBackend {
Box::new(self.0.load(req).map(move |state| {
if let Some((state, value)) = state {
RedisSession {
inner,
state,
changed: false,
inner: inner,
state: state,
value: Some(value),
}
} else {
RedisSession {
inner,
changed: false,
inner: inner,
state: HashMap::new(),
value: None,
}
@ -126,7 +127,7 @@ struct Inner {
key: Key,
ttl: String,
name: String,
addr: Addr<Unsync, RedisActor>,
addr: Addr<RedisActor>,
}
impl Inner {
@ -136,7 +137,7 @@ impl Inner {
) -> Box<Future<Item = Option<(HashMap<String, String>, String)>, Error = Error>>
{
if let Ok(cookies) = req.cookies() {
for cookie in cookies {
for cookie in cookies.iter() {
if cookie.name() == self.name {
let mut jar = CookieJar::new();
jar.add_original(cookie.clone());
@ -151,8 +152,7 @@ impl Inner {
match val {
RespValue::Error(err) => {
return Err(
error::ErrorInternalServerError(err)
.into(),
error::ErrorInternalServerError(err),
)
}
RespValue::SimpleString(s) => {
@ -173,7 +173,7 @@ impl Inner {
Ok(None)
}
Err(err) => {
Err(error::ErrorInternalServerError(err).into())
Err(error::ErrorInternalServerError(err))
}
}),
);
@ -194,7 +194,10 @@ impl Inner {
(value.clone(), None)
} else {
let mut rng = rand::OsRng::new().unwrap();
let value = String::from_iter(rng.gen_ascii_chars().take(32));
let value: String = iter::repeat(())
.map(|()| rng.sample(Alphanumeric))
.take(32)
.collect();
let mut cookie = Cookie::new(self.name.clone(), value.clone());
cookie.set_path("/");
@ -211,9 +214,7 @@ impl Inner {
Err(e) => Either::A(FutErr(e.into())),
Ok(body) => Either::B(
self.addr
.send(Command(resp_array![
"SET", value, body, "EX", &self.ttl
]))
.send(Command(resp_array!["SET", value, body, "EX", &self.ttl]))
.map_err(Error::from)
.and_then(move |res| match res {
Ok(_) => {
@ -226,7 +227,7 @@ impl Inner {
}
Ok(resp)
}
Err(err) => Err(error::ErrorInternalServerError(err).into()),
Err(err) => Err(error::ErrorInternalServerError(err)),
}),
),
})