diff --git a/Cargo.toml b/Cargo.toml index ee0cc932..9d4efd26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,11 +20,9 @@ futures-core = "0.3" futures-util = "0.3" futures-sink = "0.3" keyed_priority_queue = "0.4" -lazy_static = "1" lru = "0.12.0" mio = { version = "0.8.0", features = ["os-poll", "net"] } mysql_common = { version = "0.32", default-features = false } -once_cell = "1.7.2" pem = "3.0" percent-encoding = "2.1.0" pin-project = "1.0.2" diff --git a/src/conn/mod.rs b/src/conn/mod.rs index 1e8027cf..df6bc020 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -581,7 +581,7 @@ impl Conn { ); // Serialize here to satisfy borrow checker. - let mut buf = crate::BUFFER_POOL.get(); + let mut buf = crate::buffer_pool().get(); handshake_response.serialize(buf.as_mut()); self.write_packet(buf).await?; @@ -633,7 +633,7 @@ impl Conn { if let Some(plugin_data) = plugin_data { self.write_struct(&plugin_data.into_owned()).await?; } else { - self.write_packet(crate::BUFFER_POOL.get()).await?; + self.write_packet(crate::buffer_pool().get()).await?; } self.continue_auth().await?; @@ -701,7 +701,7 @@ impl Conn { } Some(0x04) => { let pass = self.inner.opts.pass().unwrap_or_default(); - let mut pass = crate::BUFFER_POOL.get_with(pass.as_bytes()); + let mut pass = crate::buffer_pool().get_with(pass.as_bytes()); pass.as_mut().push(0); if self.is_secure() || self.is_socket() { @@ -838,13 +838,13 @@ impl Conn { /// Writes bytes to a server. pub(crate) async fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> { - let buf = crate::BUFFER_POOL.get_with(bytes); + let buf = crate::buffer_pool().get_with(bytes); self.write_packet(buf).await } /// Sends a serializable structure to a server. pub(crate) async fn write_struct(&mut self, x: &T) -> Result<()> { - let mut buf = crate::BUFFER_POOL.get(); + let mut buf = crate::buffer_pool().get(); x.serialize(buf.as_mut()); self.write_packet(buf).await } @@ -870,7 +870,7 @@ impl Conn { T: AsRef<[u8]>, { let cmd_data = cmd_data.as_ref(); - let mut buf = crate::BUFFER_POOL.get(); + let mut buf = crate::buffer_pool().get(); let body = buf.as_mut(); body.push(cmd as u8); body.extend_from_slice(cmd_data); diff --git a/src/io/mod.rs b/src/io/mod.rs index da2be2fb..4bb27e9e 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -75,7 +75,7 @@ impl Default for PacketCodec { fn default() -> Self { Self { inner: Default::default(), - decode_buf: crate::BUFFER_POOL.get(), + decode_buf: crate::buffer_pool().get(), } } } @@ -100,7 +100,7 @@ impl Decoder for PacketCodec { fn decode(&mut self, src: &mut BytesMut) -> std::result::Result, IoError> { if self.inner.decode(src, self.decode_buf.as_mut())? { - let new_buf = crate::BUFFER_POOL.get(); + let new_buf = crate::buffer_pool().get(); Ok(Some(replace(&mut self.decode_buf, new_buf))) } else { Ok(None) diff --git a/src/lib.rs b/src/lib.rs index 01e01457..679d08e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -453,8 +453,11 @@ mod queryable; type BoxFuture<'a, T> = futures_core::future::BoxFuture<'a, Result>; -static BUFFER_POOL: once_cell::sync::Lazy> = - once_cell::sync::Lazy::new(Default::default); +fn buffer_pool() -> &'static Arc { + static BUFFER_POOL: std::sync::OnceLock> = + std::sync::OnceLock::new(); + BUFFER_POOL.get_or_init(Default::default) +} #[cfg(feature = "binlog")] #[doc(inline)] @@ -608,9 +611,8 @@ pub mod prelude { #[doc(hidden)] pub mod test_misc { - use lazy_static::lazy_static; - use std::env; + use std::sync::OnceLock; use crate::opts::{Opts, OptsBuilder, SslOpts}; @@ -621,26 +623,17 @@ pub mod test_misc { _dummy(err); } - lazy_static! { - pub static ref DATABASE_URL: String = { + pub fn get_opts() -> OptsBuilder { + static DATABASE_OPTS: OnceLock = OnceLock::new(); + let database_opts = DATABASE_OPTS.get_or_init(|| { if let Ok(url) = env::var("DATABASE_URL") { - let opts = Opts::from_url(&url).expect("DATABASE_URL invalid"); - if opts - .db_name() - .expect("a database name is required") - .is_empty() - { - panic!("database name is empty"); - } - url + Opts::from_url(&url).expect("DATABASE_URL invalid") } else { - "mysql://root:password@localhost:3307/mysql".into() + Opts::from_url("mysql://root:password@localhost:3307/mysql").unwrap() } - }; - } + }); - pub fn get_opts() -> OptsBuilder { - let mut builder = OptsBuilder::from_opts(Opts::from_url(&DATABASE_URL).unwrap()); + let mut builder = OptsBuilder::from_opts(database_opts.clone()); if test_ssl() { let ssl_opts = SslOpts::default() .with_danger_skip_domain_validation(true)