| 65 | |
| 66 | impl Worker { |
| 67 | pub(super) fn start_new(cache_config: &CacheConfig) -> Self { |
| 68 | let queue_size = match cache_config.worker_event_queue_size() { |
| 69 | num if num <= usize::max_value() as u64 => num as usize, |
| 70 | _ => usize::max_value(), |
| 71 | }; |
| 72 | let (tx, rx) = sync_channel(queue_size); |
| 73 | |
| 74 | #[cfg(test)] |
| 75 | let stats = Arc::new((Mutex::new(WorkerStats::default()), Condvar::new())); |
| 76 | |
| 77 | let worker_thread = WorkerThread { |
| 78 | receiver: rx, |
| 79 | cache_config: cache_config.clone(), |
| 80 | #[cfg(test)] |
| 81 | stats: stats.clone(), |
| 82 | }; |
| 83 | |
| 84 | // when self is dropped, sender will be dropped, what will cause the channel |
| 85 | // to hang, and the worker thread to exit -- it happens in the tests |
| 86 | // non-tests binary has only a static worker, so Rust doesn't drop it |
| 87 | thread::spawn(move || worker_thread.run()); |
| 88 | |
| 89 | Self { |
| 90 | sender: tx, |
| 91 | #[cfg(test)] |
| 92 | stats, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub(super) fn on_cache_get_async(&self, path: impl AsRef<Path>) { |
| 97 | let event = CacheEvent::OnCacheGet(path.as_ref().to_path_buf()); |