瀏覽代碼

update actix to 0.4 latest stable release (#3340)

Nikolay Kim 7 年之前
父節點
當前提交
627530f85b

文件差異過大導致無法顯示
+ 414 - 109
frameworks/Rust/actix/Cargo.lock


+ 3 - 3
frameworks/Rust/actix/Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "actix"
-version = "0.1.1"
+version = "0.2.0"
 build = "build.rs"
 
 [[bin]]
@@ -24,8 +24,8 @@ rand = "0.3"
 num_cpus = "1.0"
 futures = "0.1"
 http = "0.1"
-actix = "^0.4.3"
-actix-web = "0.3"
+actix = "0.5"
+actix-web = "0.4"
 diesel = { version = "1.1", features = ["postgres"] }
 postgres = "0.15"
 

+ 12 - 16
frameworks/Rust/actix/src/db.rs

@@ -30,13 +30,12 @@ impl DbExecutor {
 
 pub struct RandomWorld;
 
-impl ResponseType for RandomWorld {
-    type Item = models::World;
-    type Error = io::Error;
+impl Message for RandomWorld {
+    type Result = io::Result<models::World>;
 }
 
 impl Handler<RandomWorld> for DbExecutor {
-    type Result = MessageResult<RandomWorld>;
+    type Result = io::Result<models::World>;
 
     fn handle(&mut self, _: RandomWorld, _: &mut Self::Context) -> Self::Result {
         use schema::World::dsl::*;
@@ -53,13 +52,12 @@ impl Handler<RandomWorld> for DbExecutor {
 
 pub struct RandomWorlds(pub u16);
 
-impl ResponseType for RandomWorlds {
-    type Item = Vec<models::World>;
-    type Error = io::Error;
+impl Message for RandomWorlds {
+    type Result = io::Result<Vec<models::World>>;
 }
 
 impl Handler<RandomWorlds> for DbExecutor {
-    type Result = MessageResult<RandomWorlds>;
+    type Result = io::Result<Vec<models::World>>;
 
     fn handle(&mut self, msg: RandomWorlds, _: &mut Self::Context) -> Self::Result {
         use schema::World::dsl::*;
@@ -79,15 +77,14 @@ impl Handler<RandomWorlds> for DbExecutor {
 
 pub struct UpdateWorld(pub usize);
 
-impl ResponseType for UpdateWorld {
-    type Item = Vec<models::World>;
-    type Error = io::Error;
+impl Message for UpdateWorld {
+    type Result = io::Result<Vec<models::World>>;
 }
 
 impl Handler<UpdateWorld> for DbExecutor {
-    type Result = MessageResult<UpdateWorld>;
+    type Result = io::Result<Vec<models::World>>;
 
-    fn handle(&mut self, msg: UpdateWorld, _: &mut Self::Context) -> MessageResult<UpdateWorld> {
+    fn handle(&mut self, msg: UpdateWorld, _: &mut Self::Context) -> Self::Result {
         use schema::World::dsl::*;
 
         let mut worlds = Vec::with_capacity(msg.0);
@@ -112,9 +109,8 @@ impl Handler<UpdateWorld> for DbExecutor {
 
 pub struct TellFortune;
 
-impl ResponseType for TellFortune {
-    type Item = Vec<models::Fortune>;
-    type Error = io::Error;
+impl Message for TellFortune {
+    type Result = io::Result<Vec<models::Fortune>>;
 }
 
 impl Handler<TellFortune> for DbExecutor {

+ 6 - 6
frameworks/Rust/actix/src/main.rs

@@ -22,11 +22,11 @@ mod schema;
 mod models;
 
 struct State {
-    db: SyncAddress<db::DbExecutor>
+    db: Addr<Syn, db::DbExecutor>
 }
 
 fn world_row(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
-    req.state().db.call_fut(db::RandomWorld)
+    req.state().db.send(db::RandomWorld)
         .from_err()
         .and_then(|res| {
             match res {
@@ -54,7 +54,7 @@ fn queries(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
     let q = cmp::min(500, cmp::max(1, q));
 
     // run sql queries
-    req.state().db.call_fut(db::RandomWorlds(q))
+    req.state().db.send(db::RandomWorlds(q))
         .from_err()
         .and_then(|res| {
             if let Ok(worlds) = res {
@@ -81,7 +81,7 @@ fn updates(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
     let q = cmp::min(500, cmp::max(1, q));
 
     // update worlds
-    req.state().db.call_fut(db::UpdateWorld(q))
+    req.state().db.send(db::UpdateWorld(q))
         .from_err()
         .and_then(move |res| {
             if let Ok(worlds) = res {
@@ -105,7 +105,7 @@ struct FortuneTemplate<'a> {
 }
 
 fn fortune(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
-    req.state().db.call_fut(db::TellFortune)
+    req.state().db.send(db::TellFortune)
         .from_err()
         .and_then(|res| {
             match res {
@@ -136,7 +136,7 @@ fn main() {
 
     // Start db executor actors
     let addr = SyncArbiter::start(
-        num_cpus::get() * 3, move || db::DbExecutor::new(&db_url));
+        num_cpus::get() * 4, move || db::DbExecutor::new(&db_url));
 
     // start http server
     HttpServer::new(

+ 20 - 24
frameworks/Rust/actix/src/pg.rs

@@ -33,11 +33,11 @@ pub struct Fortune {
 
 
 struct State {
-    db: SyncAddress<PgConnection>
+    db: Addr<Syn, PgConnection>
 }
 
 fn world_row(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
-    req.state().db.call_fut(RandomWorld)
+    req.state().db.send(RandomWorld)
         .from_err()
         .and_then(|res| {
             match res {
@@ -64,7 +64,7 @@ fn queries(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
     let q = cmp::min(500, cmp::max(1, q));
 
     // run sql queries
-    req.state().db.call_fut(RandomWorlds(q))
+    req.state().db.send(RandomWorlds(q))
         .from_err()
         .and_then(|res| {
             if let Ok(worlds) = res {
@@ -91,7 +91,7 @@ fn updates(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error
     let q = cmp::min(500, cmp::max(1, q));
 
     // update db
-    req.state().db.call_fut(UpdateWorld(q))
+    req.state().db.send(UpdateWorld(q))
         .from_err()
         .and_then(move |res| {
             if let Ok(worlds) = res {
@@ -115,7 +115,7 @@ struct FortuneTemplate<'a> {
 }
 
 fn fortune(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
-    req.state().db.call_fut(TellFortune)
+    req.state().db.send(TellFortune)
         .from_err()
         .and_then(|res| {
             match res {
@@ -150,7 +150,7 @@ impl PgConnection {
         let conn = Connection::connect(db_url, TlsMode::None)
             .expect(&format!("Error connecting to {}", db_url));
         PgConnection{
-            conn: conn,
+            conn,
             rng: thread_rng(),
         }
     }
@@ -161,13 +161,12 @@ unsafe impl Send for PgConnection {}
 
 pub struct RandomWorld;
 
-impl ResponseType for RandomWorld {
-    type Item = World;
-    type Error = io::Error;
+impl Message for RandomWorld {
+    type Result = io::Result<World>;
 }
 
 impl Handler<RandomWorld> for PgConnection {
-    type Result = MessageResult<RandomWorld>;
+    type Result = io::Result<World>;
 
     fn handle(&mut self, _: RandomWorld, _: &mut Self::Context) -> Self::Result {
         let random_world = self.conn.prepare_cached(
@@ -178,19 +177,18 @@ impl Handler<RandomWorld> for PgConnection {
             return Ok(World {id: row.get(0), randomnumber: row.get(1)})
         }
 
-        Err(io::Error::new(io::ErrorKind::Other, format!("Database error")))
+        Err(io::Error::new(io::ErrorKind::Other, "Database error"))
     }
 }
 
 pub struct RandomWorlds(pub u16);
 
-impl ResponseType for RandomWorlds {
-    type Item = Vec<World>;
-    type Error = io::Error;
+impl Message for RandomWorlds {
+    type Result = io::Result<Vec<World>>;
 }
 
 impl Handler<RandomWorlds> for PgConnection {
-    type Result = MessageResult<RandomWorlds>;
+    type Result = io::Result<Vec<World>>;
 
     fn handle(&mut self, msg: RandomWorlds, _: &mut Self::Context) -> Self::Result {
         let random_world = self.conn.prepare_cached(
@@ -210,15 +208,14 @@ impl Handler<RandomWorlds> for PgConnection {
 
 pub struct UpdateWorld(pub u16);
 
-impl ResponseType for UpdateWorld {
-    type Item = Vec<World>;
-    type Error = io::Error;
+impl Message for UpdateWorld {
+    type Result = io::Result<Vec<World>>;
 }
 
 impl Handler<UpdateWorld> for PgConnection {
-    type Result = MessageResult<UpdateWorld>;
+    type Result = io::Result<Vec<World>>;
 
-    fn handle(&mut self, msg: UpdateWorld, _: &mut Self::Context) -> MessageResult<UpdateWorld> {
+    fn handle(&mut self, msg: UpdateWorld, _: &mut Self::Context) -> Self::Result {
         let get_world = self.conn.prepare_cached(
             "SELECT id FROM World WHERE id=$1").unwrap();
         let update_world = self.conn.prepare_cached(
@@ -241,9 +238,8 @@ impl Handler<UpdateWorld> for PgConnection {
 
 pub struct TellFortune;
 
-impl ResponseType for TellFortune {
-    type Item = Vec<Fortune>;
-    type Error = io::Error;
+impl Message for TellFortune {
+    type Result = io::Result<Vec<Fortune>>;
 }
 
 impl Handler<TellFortune> for PgConnection {
@@ -277,7 +273,7 @@ fn main() {
 
     // Start db executor actors
     let addr = SyncArbiter::start(
-        num_cpus::get() * 3, move || PgConnection::new(&db_url));
+        num_cpus::get() * 4, move || PgConnection::new(&db_url));
 
     // start http server
     HttpServer::new(

+ 2 - 2
frameworks/Rust/actix/src/simple.rs

@@ -46,8 +46,8 @@ fn main() {
     // start http server
     HttpServer::new(
         move || Application::new()
-            .resource("/json", |r| r.f(json))
-            .resource("/plaintext", |r| r.f(plaintext)))
+            .resource("/plaintext", |r| r.f(plaintext))
+            .resource("/json", |r| r.f(json)))
         .backlog(8192)
         .bind("0.0.0.0:8080").unwrap()
         .start();

部分文件因文件數量過多而無法顯示