Execute State functions in thread pool
This commit is contained in:
parent
b3e7552c16
commit
6fb1062376
4 changed files with 62 additions and 52 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -9,6 +9,7 @@ dependencies = [
|
|||
"diesel 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"diesel_codegen 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures-cpupool 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"hyper 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libsqlite3-sys 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -5,6 +5,7 @@ authors = ["Magnus Hoff <maghoff@gmail.com>"]
|
|||
|
||||
[dependencies]
|
||||
futures = "0.1"
|
||||
futures-cpupool = "0.1"
|
||||
hyper = "0.11"
|
||||
tokio-io = "0.1"
|
||||
tokio-proto = "0.1"
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
extern crate chrono;
|
||||
extern crate clap;
|
||||
extern crate futures;
|
||||
extern crate futures_cpupool;
|
||||
extern crate hyper;
|
||||
extern crate pulldown_cmark;
|
||||
extern crate r2d2;
|
||||
|
|
31
src/state.rs
31
src/state.rs
|
@ -3,7 +3,7 @@ use std;
|
|||
use diesel;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use diesel::prelude::*;
|
||||
use futures::{Future, IntoFuture, BoxFuture};
|
||||
use futures_cpupool::{self, CpuFuture};
|
||||
use r2d2::Pool;
|
||||
use r2d2_diesel::ConnectionManager;
|
||||
|
||||
|
@ -11,33 +11,41 @@ use models;
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
connection_pool: Pool<ConnectionManager<SqliteConnection>>
|
||||
connection_pool: Pool<ConnectionManager<SqliteConnection>>,
|
||||
cpu_pool: futures_cpupool::CpuPool,
|
||||
}
|
||||
|
||||
pub type Error = Box<std::error::Error + Send + Sync>;
|
||||
|
||||
impl State {
|
||||
pub fn new(connection_pool: Pool<ConnectionManager<SqliteConnection>>) -> State {
|
||||
State { connection_pool }
|
||||
State {
|
||||
connection_pool,
|
||||
cpu_pool: futures_cpupool::CpuPool::new_num_cpus(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_article_revision_by_id(&self, article_id: i32) -> BoxFuture<Option<models::ArticleRevision>, Error> {
|
||||
(|| -> Result<_, _> {
|
||||
pub fn get_article_revision_by_id(&self, article_id: i32) -> CpuFuture<Option<models::ArticleRevision>, Error> {
|
||||
let connection_pool = self.connection_pool.clone();
|
||||
|
||||
self.cpu_pool.spawn_fn(move || {
|
||||
use schema::article_revisions;
|
||||
|
||||
Ok(article_revisions::table
|
||||
.filter(article_revisions::article_id.eq(article_id))
|
||||
.order(article_revisions::revision.desc())
|
||||
.limit(1)
|
||||
.load::<models::ArticleRevision>(&*self.connection_pool.get()?)?
|
||||
.load::<models::ArticleRevision>(&*connection_pool.get()?)?
|
||||
.pop())
|
||||
}()).into_future().boxed()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update_article(&self, article_id: i32, base_revision: i32, body: String) -> BoxFuture<models::ArticleRevision, Error> {
|
||||
self.connection_pool.get().into_future()
|
||||
.map_err(Into::into)
|
||||
.and_then(move |conn| {
|
||||
pub fn update_article(&self, article_id: i32, base_revision: i32, body: String) -> CpuFuture<models::ArticleRevision, Error> {
|
||||
let connection_pool = self.connection_pool.clone();
|
||||
|
||||
self.cpu_pool.spawn_fn(move || {
|
||||
let conn = connection_pool.get()?;
|
||||
|
||||
conn.transaction(|| {
|
||||
use schema::article_revisions;
|
||||
|
||||
|
@ -84,6 +92,5 @@ impl State {
|
|||
)
|
||||
})
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue