| 378 | } |
| 379 | |
| 380 | fn expiry(&mut self) -> Result<()> { |
| 381 | // Cleanup bitswap sessions |
| 382 | let mut to_remove = Vec::new(); |
| 383 | for (session_id, workers) in &mut self.bitswap_sessions { |
| 384 | // Check if the workers are still active |
| 385 | workers.retain(|(_, worker)| !worker.is_finished()); |
| 386 | |
| 387 | if workers.is_empty() { |
| 388 | to_remove.push(*session_id); |
| 389 | } |
| 390 | |
| 391 | // Only do a small chunk of cleanup on each iteration |
| 392 | // TODO(arqu): magic number |
| 393 | if to_remove.len() >= 10 { |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | for session_id in to_remove { |
| 399 | let (s, _r) = oneshot::channel(); |
| 400 | self.destroy_session(session_id, s); |
| 401 | } |
| 402 | |
| 403 | Ok(()) |
| 404 | } |
| 405 | |
| 406 | /// Subscribe to [`NetworkEvent`]s. |
| 407 | #[tracing::instrument(skip(self))] |