Skip to content

Commit

Permalink
Switch from lazy_static to once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Sep 5, 2019
1 parent d53bc2c commit fd1346e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ runtime = ["hyper/runtime"]
antidote = "1.0.0"
bytes = "0.4"
hyper = { version = "=0.13.0-alpha.1", default-features = false }
lazy_static = "1.0"
linked_hash_set = "0.1"
once_cell = "1.0"
openssl = "0.10.19"
openssl-sys = "0.9.26"
tokio = "=0.2.0-alpha.4"
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ use cache::{SessionCache, SessionKey};
use std::future::Future;
use std::pin::Pin;
use std::task::{Poll, Context};
use once_cell::sync::OnceCell;

mod cache;
#[cfg(test)]
mod test;

lazy_static::lazy_static! {
// The unwrap here isn't great but this only fails on OOM
static ref KEY_INDEX: Index<Ssl, SessionKey> = Ssl::new_ex_index().unwrap();
fn key_index() -> Result<Index<Ssl, SessionKey>, ErrorStack> {
static IDX: OnceCell<Index<Ssl, SessionKey>> = OnceCell::new();
IDX.get_or_try_init(|| Ssl::new_ex_index()).map(|v| *v)
}

#[derive(Clone)]
Expand Down Expand Up @@ -63,7 +64,8 @@ impl Inner {
}
}

conf.set_ex_data(*KEY_INDEX, key);
let idx = key_index()?;
conf.set_ex_data(idx, key);

Ok(conf)
}
Expand Down Expand Up @@ -117,7 +119,7 @@ where
ssl.set_new_session_callback({
let cache = cache.clone();
move |ssl, session| {
if let Some(key) = ssl.ex_data(*KEY_INDEX) {
if let Some(key) = key_index().ok().and_then(|idx| ssl.ex_data(idx)) {
cache.lock().insert(key.clone(), session);
}
}
Expand Down

0 comments on commit fd1346e

Please sign in to comment.