diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-02-24 17:18:09 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-02-24 17:18:09 +0100 |
| commit | db0f14a163a087261db7360a90d6ac70fb92e884 (patch) | |
| tree | 4963b1342bba7e0b2123fa33b7a683141ab43a25 | |
| parent | 288eadd3c42352deba28cfe062e79e673cb5d577 (diff) | |
| download | frippy-db0f14a163a087261db7360a90d6ac70fb92e884.tar.gz frippy-db0f14a163a087261db7360a90d6ac70fb92e884.zip | |
Use r2d2 as a ConnectionPool for diesel
| -rw-r--r-- | Cargo.lock | 38 | ||||
| -rw-r--r-- | Cargo.toml | 17 | ||||
| -rw-r--r-- | src/lib.rs | 4 | ||||
| -rw-r--r-- | src/main.rs | 19 | ||||
| -rw-r--r-- | src/plugins/factoids/database.rs | 46 | ||||
| -rw-r--r-- | src/plugins/factoids/mod.rs | 20 |
6 files changed, 109 insertions, 35 deletions
@@ -12,6 +12,11 @@ dependencies = [ ] [[package]] +name = "antidote" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "backtrace" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -391,6 +396,8 @@ dependencies = [ "irc 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", + "r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)", "rlua 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -928,6 +935,25 @@ version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "r2d2" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "scheduled-thread-pool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "r2d2-diesel" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "diesel 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "rand" version = "0.3.22" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1045,6 +1071,14 @@ dependencies = [ ] [[package]] +name = "scheduled-thread-pool" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "scoped-tls" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1483,6 +1517,7 @@ dependencies = [ [metadata] "checksum adler32 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6cbd0b9af8587c72beadc9f72d35b9fbb070982c9e6203e46e93f10df25f8f45" "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" +"checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5" "checksum backtrace 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ebbbf59b1c43eefa8c3ede390fcc36820b4999f7914104015be25025e0d62af2" "checksum backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "44585761d6161b0f57afc49482ab6bd067e4edef48c12a152c237eb0203f7661" "checksum base64 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "229d032f1a99302697f10b27167ae6d03d49d032e6a8e2550e8d3fc13356d2b4" @@ -1588,6 +1623,8 @@ dependencies = [ "checksum pulldown-cmark 0.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "378e941dbd392c101f2cb88097fa4d7167bc421d4b88de3ff7dbee503bc3233b" "checksum quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" +"checksum r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f9078ca6a8a5568ed142083bb2f7dc9295b69d16f867ddcc9849e51b17d8db46" +"checksum r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9c29bad92da76d02bc2c020452ebc3a3fe6fa74cfab91e711c43116e4fb1a3" "checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" @@ -1601,6 +1638,7 @@ dependencies = [ "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f" "checksum schannel 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "acece75e0f987c48863a6c792ec8b7d6c4177d4a027f8ccc72f849794f437016" +"checksum scheduled-thread-pool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a2ff3fc5223829be817806c6441279c676e454cc7da608faf03b0ccc09d3889" "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d" "checksum security-framework 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "dfa44ee9c54ce5eecc9de7d5acbad112ee58755239381f687e564004ba4a2332" "checksum security-framework-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "5421621e836278a0b139268f36eee0dc7e389b784dc3f79d8f11aabadf41bead" @@ -14,7 +14,14 @@ name = "frippy" doc = false [features] -mysql = ["diesel", "diesel_infer_schema", "diesel_migrations", "dotenv"] +mysql = [ + "diesel", + "diesel_infer_schema", + "diesel_migrations", + "dotenv", + "r2d2", + "r2d2-diesel" +] [dependencies] irc = "0.13.4" @@ -52,6 +59,14 @@ version = "1.1.0" optional = true features = ["mysql"] +[dependencies.r2d2] +version = "0.8.2" +optional = true + +[dependencies.r2d2-diesel] +version = "1.0.0" +optional = true + [dependencies.dotenv] version = "0.11.0" optional = true @@ -32,6 +32,10 @@ #[cfg(feature = "mysql")] #[macro_use] extern crate diesel; +#[cfg(feature = "mysql")] +extern crate r2d2; +#[cfg(feature = "mysql")] +extern crate r2d2_diesel; #[macro_use] extern crate frippy_derive; diff --git a/src/main.rs b/src/main.rs index cb4e384..6605693 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,10 @@ extern crate time; extern crate diesel_migrations; #[cfg(feature = "mysql")] extern crate diesel; +#[cfg(feature = "mysql")] +extern crate r2d2; +#[cfg(feature = "mysql")] +extern crate r2d2_diesel; #[macro_use] extern crate log; @@ -119,13 +123,16 @@ fn main() { #[cfg(feature = "mysql")] { if let Some(url) = mysql_url { - use diesel; - use diesel::Connection; - match diesel::mysql::MysqlConnection::establish(url) { - Ok(conn) => { - match embedded_migrations::run(&conn) { + use diesel::MysqlConnection; + use r2d2; + use r2d2_diesel::ConnectionManager; + + let manager = ConnectionManager::<MysqlConnection>::new(url.clone()); + match r2d2::Pool::builder().build(manager) { + Ok(pool) => { + match embedded_migrations::run(&*pool.get().expect("Failed to get connection")) { Ok(_) => { - bot.add_plugin(plugins::Factoids::new(conn)); + bot.add_plugin(plugins::Factoids::new(pool)); info!("Connected to MySQL server") } Err(e) => { diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs index cb5ef29..f003419 100644 --- a/src/plugins/factoids/database.rs +++ b/src/plugins/factoids/database.rs @@ -5,9 +5,12 @@ use std::collections::HashMap; #[cfg(feature = "mysql")] use diesel::prelude::*; - #[cfg(feature = "mysql")] use diesel::mysql::MysqlConnection; +#[cfg(feature = "mysql")] +use r2d2::Pool; +#[cfg(feature = "mysql")] +use r2d2_diesel::ConnectionManager; use chrono::NaiveDateTime; @@ -40,15 +43,15 @@ pub struct NewFactoid<'a> { pub trait Database: Send { - fn insert(&mut self, factoid: &NewFactoid) -> DbResponse; - fn get(&self, name: &str, idx: i32) -> Option<Factoid>; - fn delete(&mut self, name: &str, idx: i32) -> DbResponse; - fn count(&self, name: &str) -> Result<i32, &'static str>; + fn insert_factoid(&mut self, factoid: &NewFactoid) -> DbResponse; + fn get_factoid(&self, name: &str, idx: i32) -> Option<Factoid>; + fn delete_factoid(&mut self, name: &str, idx: i32) -> DbResponse; + fn count_factoids(&self, name: &str) -> Result<i32, &'static str>; } // HashMap impl Database for HashMap<(String, i32), Factoid> { - fn insert(&mut self, factoid: &NewFactoid) -> DbResponse { + fn insert_factoid(&mut self, factoid: &NewFactoid) -> DbResponse { let factoid = Factoid { name: String::from(factoid.name), idx: factoid.idx, @@ -64,18 +67,18 @@ impl Database for HashMap<(String, i32), Factoid> { } } - fn get(&self, name: &str, idx: i32) -> Option<Factoid> { + fn get_factoid(&self, name: &str, idx: i32) -> Option<Factoid> { self.get(&(String::from(name), idx)).map(|f| f.clone()) } - fn delete(&mut self, name: &str, idx: i32) -> DbResponse { + fn delete_factoid(&mut self, name: &str, idx: i32) -> DbResponse { match self.remove(&(String::from(name), idx)) { Some(_) => DbResponse::Success, None => DbResponse::Failed("Factoid not found"), } } - fn count(&self, name: &str) -> Result<i32, &'static str> { + fn count_factoids(&self, name: &str) -> Result<i32, &'static str> { Ok(self.iter() .filter(|&(&(ref n, _), _)| n == name) .count() as i32) @@ -98,12 +101,14 @@ mod mysql { } #[cfg(feature = "mysql")] -impl Database for MysqlConnection { - fn insert(&mut self, factoid: &NewFactoid) -> DbResponse { +impl Database for Pool<ConnectionManager<MysqlConnection>> { + fn insert_factoid(&mut self, factoid: &NewFactoid) -> DbResponse { use diesel; + + let conn = &*self.get().expect("Failed to get connection"); match diesel::insert_into(factoids::table) .values(factoid) - .execute(self) { + .execute(conn) { Ok(_) => DbResponse::Success, Err(e) => { error!("DB Insertion Error: {}", e); @@ -112,8 +117,9 @@ impl Database for MysqlConnection { } } - fn get(&self, name: &str, idx: i32) -> Option<Factoid> { - match factoids::table.find((name, idx)).first(self) { + fn get_factoid(&self, name: &str, idx: i32) -> Option<Factoid> { + let conn = &*self.get().expect("Failed to get connection"); + match factoids::table.find((name, idx)).first(conn) { Ok(f) => Some(f), Err(e) => { error!("DB Count Error: {}", e); @@ -122,13 +128,15 @@ impl Database for MysqlConnection { } } - fn delete(&mut self, name: &str, idx: i32) -> DbResponse { + fn delete_factoid(&mut self, name: &str, idx: i32) -> DbResponse { use diesel; use self::factoids::columns; + + let conn = &*self.get().expect("Failed to get connection"); match diesel::delete(factoids::table .filter(columns::name.eq(name)) .filter(columns::idx.eq(idx))) - .execute(self) { + .execute(conn) { Ok(v) => { if v > 0 { DbResponse::Success @@ -143,11 +151,13 @@ impl Database for MysqlConnection { } } - fn count(&self, name: &str) -> Result<i32, &'static str> { + fn count_factoids(&self, name: &str) -> Result<i32, &'static str> { + + let conn = &*self.get().expect("Failed to get connection"); let count: Result<i64, _> = factoids::table .filter(factoids::columns::name.eq(name)) .count() - .first(self); + .first(conn); match count { Ok(c) => Ok(c as i32), diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index 49ace10..b662d22 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -39,7 +39,7 @@ impl<T: Database> Factoids<T> { } fn create_factoid(&self, name: &str, content: &str, author: &str) -> Result<&str, &str> { - let count = try_lock!(self.factoids).count(&name)?; + let count = try_lock!(self.factoids).count_factoids(&name)?; let tm = time::now().to_timespec(); let factoid = database::NewFactoid { @@ -50,7 +50,7 @@ impl<T: Database> Factoids<T> { created: NaiveDateTime::from_timestamp(tm.sec, tm.nsec as u32), }; - match try_lock!(self.factoids).insert(&factoid) { + match try_lock!(self.factoids).insert_factoid(&factoid) { DbResponse::Success => Ok("Successfully added"), DbResponse::Failed(e) => Err(e), } @@ -93,12 +93,12 @@ impl<T: Database> Factoids<T> { } let name = command.tokens.remove(0); - let count = match try_lock!(self.factoids).count(&name) { + let count = match try_lock!(self.factoids).count_factoids(&name) { Ok(c) => c, Err(e) => return client.send_notice(&command.source, e), }; - match try_lock!(self.factoids).delete(&name, count - 1) { + match try_lock!(self.factoids).delete_factoid(&name, count - 1) { DbResponse::Success => client.send_notice(&command.source, "Successfully removed"), DbResponse::Failed(e) => client.send_notice(&command.source, &e), } @@ -110,7 +110,7 @@ impl<T: Database> Factoids<T> { 0 => return self.invalid_command(client, command), 1 => { let name = &command.tokens[0]; - let count = match try_lock!(self.factoids).count(name) { + let count = match try_lock!(self.factoids).count_factoids(name) { Ok(c) => c, Err(e) => return client.send_notice(&command.source, e), }; @@ -132,7 +132,7 @@ impl<T: Database> Factoids<T> { } }; - let factoid = match try_lock!(self.factoids).get(name, idx) { + let factoid = match try_lock!(self.factoids).get_factoid(name, idx) { Some(v) => v, None => { return client.send_notice(&command.source, @@ -152,7 +152,7 @@ impl<T: Database> Factoids<T> { 0 => self.invalid_command(client, command), 1 => { let name = &command.tokens[0]; - let count = match try_lock!(self.factoids).count(name) { + let count = match try_lock!(self.factoids).count_factoids(name) { Ok(c) => c, Err(e) => return client.send_notice(&command.source, e), }; @@ -176,7 +176,7 @@ impl<T: Database> Factoids<T> { Err(_) => return client.send_notice(&command.source, "Invalid index"), }; - let factoid = match try_lock!(self.factoids).get(name, idx) { + let factoid = match try_lock!(self.factoids).get_factoid(name, idx) { Some(v) => v, None => { return client.send_notice(&command.source, @@ -204,12 +204,12 @@ impl<T: Database> Factoids<T> { } else { let name = command.tokens.remove(0); - let count = match try_lock!(self.factoids).count(&name) { + let count = match try_lock!(self.factoids).count_factoids(&name) { Ok(c) => c, Err(e) => return client.send_notice(&command.source, e), }; - let factoid = match try_lock!(self.factoids).get(&name, count - 1) { + let factoid = match try_lock!(self.factoids).get_factoid(&name, count - 1) { Some(v) => v.content, None if error => return self.invalid_command(client, &command), None => return Ok(()), |
