mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
use r2d2 for diesel example
This commit is contained in:
parent
a5a36ff194
commit
b2e771df2c
@ -15,5 +15,7 @@ serde = "1.0"
|
|||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
|
||||||
diesel = { version = "1.0.0-beta1", features = ["sqlite"] }
|
diesel = { version = "1.0", features = ["sqlite"] }
|
||||||
|
r2d2 = "0.8"
|
||||||
|
r2d2-diesel = "1.0"
|
||||||
dotenv = "0.10"
|
dotenv = "0.10"
|
||||||
|
@ -4,12 +4,14 @@ use diesel;
|
|||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use r2d2::Pool;
|
||||||
|
use r2d2_diesel::ConnectionManager;
|
||||||
|
|
||||||
use models;
|
use models;
|
||||||
use schema;
|
use schema;
|
||||||
|
|
||||||
/// This is db executor actor. We are going to run 3 of them in parallel.
|
/// This is db executor actor. We are going to run 3 of them in parallel.
|
||||||
pub struct DbExecutor(pub SqliteConnection);
|
pub struct DbExecutor(pub Pool<ConnectionManager<SqliteConnection>>);
|
||||||
|
|
||||||
/// This is only message that this actor can handle, but it is easy to extend number of
|
/// This is only message that this actor can handle, but it is easy to extend number of
|
||||||
/// messages.
|
/// messages.
|
||||||
@ -37,14 +39,16 @@ impl Handler<CreateUser> for DbExecutor {
|
|||||||
name: &msg.name,
|
name: &msg.name,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let conn: &SqliteConnection = &self.0.get().unwrap();
|
||||||
|
|
||||||
diesel::insert_into(users)
|
diesel::insert_into(users)
|
||||||
.values(&new_user)
|
.values(&new_user)
|
||||||
.execute(&self.0)
|
.execute(conn)
|
||||||
.expect("Error inserting person");
|
.expect("Error inserting person");
|
||||||
|
|
||||||
let mut items = users
|
let mut items = users
|
||||||
.filter(id.eq(&uuid))
|
.filter(id.eq(&uuid))
|
||||||
.load::<models::User>(&self.0)
|
.load::<models::User>(conn)
|
||||||
.expect("Error loading person");
|
.expect("Error loading person");
|
||||||
|
|
||||||
Ok(items.pop().unwrap())
|
Ok(items.pop().unwrap())
|
||||||
|
@ -10,6 +10,8 @@ extern crate serde_json;
|
|||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
|
extern crate r2d2;
|
||||||
|
extern crate r2d2_diesel;
|
||||||
extern crate uuid;
|
extern crate uuid;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate actix;
|
extern crate actix;
|
||||||
@ -21,6 +23,7 @@ use actix_web::{http, middleware,
|
|||||||
App, HttpServer, HttpRequest, HttpResponse, Error, AsyncResponder};
|
App, HttpServer, HttpRequest, HttpResponse, Error, AsyncResponder};
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use r2d2_diesel::ConnectionManager;
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
|
|
||||||
mod db;
|
mod db;
|
||||||
@ -53,12 +56,15 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>>
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
let _ = env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("diesel-example");
|
let sys = actix::System::new("diesel-example");
|
||||||
|
|
||||||
// Start 3 db executor actors
|
// Start 3 db executor actors
|
||||||
let addr = SyncArbiter::start(3, || {
|
let manager = ConnectionManager::<SqliteConnection>::new("test.db");
|
||||||
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
let pool = r2d2::Pool::builder().build(manager).expect("Failed to create pool.");
|
||||||
|
|
||||||
|
let addr = SyncArbiter::start(3, move || {
|
||||||
|
DbExecutor(pool.clone())
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start http server
|
// Start http server
|
||||||
|
Loading…
Reference in New Issue
Block a user