| 46 | |
| 47 | impl PluginHandle { |
| 48 | pub fn spawn( |
| 49 | plugin: Arc<dyn Plugin>, |
| 50 | mut endpoint: ServerEndpoint, |
| 51 | config: Arc<RwLock<ClientConfig>>, |
| 52 | opts: ClientOpts, |
| 53 | to_server_tx: FairSender<Message>, |
| 54 | ) -> Self { |
| 55 | let guid = endpoint.guid.clone(); |
| 56 | let guid_for_task = guid.clone(); |
| 57 | let server = Arc::new(RwLock::new(None)); |
| 58 | let server_task = server.clone(); |
| 59 | |
| 60 | // Per-process cancel and event channels |
| 61 | let (cancel_tx, mut cancel_rx) = mpsc::channel::<Message>(1); |
| 62 | let (proc_event_tx, mut proc_event_rx) = mpsc::channel::<Message>(1024); |
| 63 | |
| 64 | let task = tokio::spawn(async move { |
| 65 | // Forward per-process events to the main client event channel |
| 66 | tokio::spawn({ |
| 67 | let mut endpoint = endpoint.clone(); |
| 68 | let to_server_tx = to_server_tx.clone(); |
| 69 | let guid_for_task = guid_for_task.clone(); |
| 70 | async move { |
| 71 | use tokio::time::{Duration, Instant}; |
| 72 | |
| 73 | let mut last_progress_time = Instant::now() - Duration::from_secs(1); // Allow first progress immediately |
| 74 | |
| 75 | while let Some(mut msg) = proc_event_rx.recv().await { |
| 76 | let should_send = match &mut msg { |
| 77 | Message::Progress(progress_info) => { |
| 78 | progress_info.guid = guid_for_task.clone(); |
| 79 | |
| 80 | let now = Instant::now(); |
| 81 | // Always send 0% and 100% progress messages, or throttle to 1 per second |
| 82 | if progress_info.current == 0 |
| 83 | || progress_info.current >= progress_info.total |
| 84 | || now.duration_since(last_progress_time) |
| 85 | >= Duration::from_secs(1) |
| 86 | { |
| 87 | last_progress_time = now; |
| 88 | true |
| 89 | } else { |
| 90 | false |
| 91 | } |
| 92 | } |
| 93 | _ => true, // Always send non-progress messages |
| 94 | }; |
| 95 | |
| 96 | if should_send { |
| 97 | to_server_tx.send(msg.clone()).await.ok(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | endpoint.status = Some("offline".to_string()); |
| 102 | to_server_tx |
| 103 | .send(Message::EndpointStatus(endpoint.clone())) |
| 104 | .await |
| 105 | .ok(); |