From 62c382d28049b84071bbd27e5c58298966a493d3 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 20 Jul 2018 14:53:56 -0700 Subject: [PATCH] upgrade to actix/actix-web 0.7 --- Cargo.toml | 13 ++++++------ examples/basic.rs | 2 +- src/lib.rs | 8 +++----- src/redis.rs | 22 ++++++++------------ src/session.rs | 29 ++++++++++++++------------- tests/test_redis.rs | 49 +++++++++++++++++++++------------------------ 6 files changed, 57 insertions(+), 66 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c12075685..5e3d3afd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-redis" -version = "0.4.0" +version = "0.5.0" authors = ["Nikolay Kim "] description = "Redis integration for actix framework" license = "MIT/Apache-2.0" @@ -27,21 +27,22 @@ default = ["web"] web = ["actix-web", "cookie", "http", "rand", "serde", "serde_json"] [dependencies] -actix = "0.5" +actix = "0.7" log = "0.4" backoff = "0.1" failure = "^0.1.1" futures = "0.1" tokio-io = "0.1" -tokio-core = "0.1" -redis-async = "0.0" +tokio-codec = "0.1" +tokio-tcp = "0.1" +redis-async = "0.3.2" # actix web session -actix-web = { version="0.6", optional=true } +actix-web = { git = "https://github.com/actix/actix-web.git", optional=true } cookie = { version="0.10", features=["percent-encode", "secure"], optional=true } http = { version="0.1", optional=true } -rand = { version="0.3", optional=true } +rand = { version="0.5", optional=true } serde = { version="1.0", optional=true } serde_json = { version="1.0", optional=true } diff --git a/examples/basic.rs b/examples/basic.rs index cb76627ec..4dbed88a7 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -12,7 +12,7 @@ use actix_web::middleware::session::{self, RequestSession}; use actix_web::{middleware, server, App, HttpRequest, HttpResponse, Result}; /// simple handler -fn index(req: HttpRequest) -> Result { +fn index(req: &HttpRequest) -> Result { println!("{:?}", req); // session diff --git a/src/lib.rs b/src/lib.rs index 71a2c86a9..5a0a96547 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 for Error { fn from(err: redis_async::error::Error) -> Error { Error::Redis(err) diff --git a/src/redis.rs b/src/redis.rs index 2fd6ab8c4..91fd4f5dc 100644 --- a/src/redis.rs +++ b/src/redis.rs @@ -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>(addr: S) -> Addr { + pub fn start>(addr: S) -> Addr { 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; fn started(&mut self, ctx: &mut Context) { - 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 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 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)) } } diff --git a/src/session.rs b/src/session.rs index 011f1ad58..91d9936c5 100644 --- a/src/session.rs +++ b/src/session.rs @@ -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 SessionBackend 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, + addr: Addr, } impl Inner { @@ -136,7 +137,7 @@ impl Inner { ) -> Box, 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)), }), ), }) diff --git a/tests/test_redis.rs b/tests/test_redis.rs index a3d650e45..a9fef4a74 100644 --- a/tests/test_redis.rs +++ b/tests/test_redis.rs @@ -16,16 +16,15 @@ fn test_error_connect() { let addr = RedisActor::start("localhost:54000"); let _addr2 = addr.clone(); - Arbiter::handle().spawn_fn(move || { - addr.send(Command(resp_array!["GET", "test"])) - .then(|res| { - match res { - Ok(Err(Error::NotConnected)) => (), - _ => panic!("Should not happen {:?}", res), - } - Arbiter::system().do_send(actix::msgs::SystemExit(0)); - Ok(()) - }) + Arbiter::spawn_fn(move || { + addr.send(Command(resp_array!["GET", "test"])).then(|res| { + match res { + Ok(Err(Error::NotConnected)) => (), + _ => panic!("Should not happen {:?}", res), + } + System::current().stop(); + Ok(()) + }) }); sys.run(); @@ -39,28 +38,26 @@ fn test_redis() { let addr = RedisActor::start("127.0.0.1:6379"); let _addr2 = addr.clone(); - Arbiter::handle().spawn_fn(move || { + Arbiter::spawn_fn(move || { let addr2 = addr.clone(); addr.send(Command(resp_array!["SET", "test", "value"])) .then(move |res| match res { Ok(Ok(resp)) => { assert_eq!(resp, RespValue::SimpleString("OK".to_owned())); - addr2 - .send(Command(resp_array!["GET", "test"])) - .then(|res| { - match res { - Ok(Ok(resp)) => { - println!("RESP: {:?}", resp); - assert_eq!( - resp, - RespValue::BulkString((&b"value"[..]).into()) - ); - } - _ => panic!("Should not happen {:?}", res), + addr2.send(Command(resp_array!["GET", "test"])).then(|res| { + match res { + Ok(Ok(resp)) => { + println!("RESP: {:?}", resp); + assert_eq!( + resp, + RespValue::BulkString((&b"value"[..]).into()) + ); } - Arbiter::system().do_send(actix::msgs::SystemExit(0)); - Ok(()) - }) + _ => panic!("Should not happen {:?}", res), + } + System::current().stop(); + Ok(()) + }) } _ => panic!("Should not happen {:?}", res), })