diff --git a/Cargo.toml b/Cargo.toml index 23f14d8..88b86d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ serde_json = {version = "1", optional = true} seahash = "4.1" wg = "0.3" thiserror = "1" +tracing = "0.1" xxhash-rust = { version = "0.8", features = ["xxh64"] } [dev-dependencies] diff --git a/src/cache/async.rs b/src/cache/async.rs index 2ed7ee8..69f4697 100644 --- a/src/cache/async.rs +++ b/src/cache/async.rs @@ -702,22 +702,23 @@ where loop { select! { item = self.insert_buf_rx.recv().fuse() => { - if let Err(_e) = self.handle_insert_event(item) { - return; + if let Err(e) = self.handle_insert_event(item) { + tracing::error!("fail to handle insert event, error: {}", e); } } _ = cleanup_timer.next().fuse() => { - if let Err(_e) = self.handle_cleanup_event() { - return; + if let Err(e) = self.handle_cleanup_event() { + tracing::error!("fail to handle cleanup event, error: {}", e); } }, _ = self.clear_rx.recv().fuse() => { - let _ = CacheCleaner::new(&mut self).clean().await; + if let Err(e) = CacheCleaner::new(&mut self).clean().await { + tracing::error!("fail to handle clear event, error: {}", e); + } }, _ = self.stop_rx.recv().fuse() => { - if let Err(_e) = self.handle_close_event() { - return; - } + _ = self.handle_close_event(); + return; }, } } diff --git a/src/cache/sync.rs b/src/cache/sync.rs index 3c7085a..dce8600 100644 --- a/src/cache/sync.rs +++ b/src/cache/sync.rs @@ -637,13 +637,19 @@ where spawn(move || loop { select! { recv(self.insert_buf_rx) -> res => { - self.handle_insert_event(res)?; + if let Err(e) = self.handle_insert_event(res) { + tracing::error!("fail to handle insert event: {}", e); + } }, recv(self.clear_rx) -> _ => { - self.handle_clear_event()?; + if let Err(e) = self.handle_clear_event() { + tracing::error!("fail to handle clear event: {}", e); + } }, recv(ticker) -> msg => { - self.handle_cleanup_event(msg)?; + if let Err(e) = self.handle_cleanup_event(msg) { + tracing::error!("fail to handle cleanup event: {}", e); + } }, recv(self.stop_rx) -> _ => return Ok(()), } diff --git a/src/ttl.rs b/src/ttl.rs index 8b1b49f..1c24bf2 100644 --- a/src/ttl.rs +++ b/src/ttl.rs @@ -1,5 +1,4 @@ use parking_lot::RwLock; -use std::cell::RefCell; use std::collections::{hash_map::RandomState, HashMap}; use std::hash::BuildHasher; use std::ops::{Deref, DerefMut}; @@ -101,7 +100,7 @@ impl DerefMut for Bucket { #[derive(Debug)] pub(crate) struct ExpirationMap { - buckets: RwLock, S>>>, + buckets: RwLock, S>>, hasher: S, } @@ -109,7 +108,7 @@ impl Default for ExpirationMap { fn default() -> Self { let hasher = RandomState::default(); Self { - buckets: RwLock::new(RefCell::new(HashMap::with_hasher(hasher.clone()))), + buckets: RwLock::new(HashMap::with_hasher(hasher.clone())), hasher, } } @@ -124,7 +123,7 @@ impl ExpirationMap { impl ExpirationMap { pub(crate) fn with_hasher(hasher: S) -> ExpirationMap { ExpirationMap { - buckets: RwLock::new(RefCell::new(HashMap::with_hasher(hasher.clone()))), + buckets: RwLock::new(HashMap::with_hasher(hasher.clone())), hasher, } } @@ -137,11 +136,8 @@ impl ExpirationMap { let bucket_num = storage_bucket(expiration); - let m = self.buckets.read(); + let mut m = self.buckets.write(); - let mut m = m - .try_borrow_mut() - .map_err(|e| CacheError::InsertError(e.to_string()))?; match m.get_mut(&bucket_num) { None => { let mut bucket = Bucket::with_hasher(self.hasher.clone()); @@ -167,7 +163,6 @@ impl ExpirationMap { return Ok(()); } - let m = self.buckets.read(); let (old_bucket_num, new_bucket_num) = (storage_bucket(old_exp_time), storage_bucket(new_exp_time)); @@ -175,9 +170,8 @@ impl ExpirationMap { return Ok(()); } - let mut m = m - .try_borrow_mut() - .map_err(|e| CacheError::UpdateError(e.to_string()))?; + let mut m = self.buckets.write(); + m.remove(&old_bucket_num); match m.get_mut(&new_bucket_num) { @@ -196,12 +190,8 @@ impl ExpirationMap { pub fn try_remove(&self, key: &u64, expiration: Time) -> Result<(), CacheError> { let bucket_num = storage_bucket(expiration); - let m = self.buckets.read(); - if let Some(bucket) = m - .try_borrow_mut() - .map_err(|e| CacheError::RemoveError(e.to_string()))? - .get_mut(&bucket_num) - { + let mut m = self.buckets.write(); + if let Some(bucket) = m.get_mut(&bucket_num) { bucket.remove(key); }; @@ -212,9 +202,7 @@ impl ExpirationMap { let bucket_num = cleanup_bucket(now); Ok(self .buckets - .read() - .try_borrow_mut() - .map_err(|e| CacheError::CleanupError(e.to_string()))? + .write() .remove(&bucket_num) .map(|bucket| bucket.map)) }