DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Vine/Client/WaitForClientConnection.rs
1//! Await Cocoon's gRPC connection without polling. `GetConnectionNotify`
2//! returns a shared `tokio::sync::Notify` that `ConnectToSideCar` fires
3//! once the handshake succeeds; `WaitForClientConnection` simply awaits
4//! it under `tokio::time::timeout`. If the sidecar is already connected
5//! when we enter (typical for the second-and-later callers) `notified()`
6//! returns immediately because `notify_waiters` has already fired.
7//!
8//! `BudgetMilliseconds` remains the hard cap so call sites keep their
9//! existing behaviour for the pathological "Cocoon never starts" case.
10
11use std::time::Duration;
12
13use crate::Vine::Client::{IsClientConnected, IsShuttingDown, Shared::GetConnectionNotify};
14
15pub async fn Fn(SideCarIdentifier:&str, BudgetMilliseconds:u64) -> bool {
16 if IsShuttingDown::Fn() {
17 return false;
18 }
19
20 if IsClientConnected::Fn(SideCarIdentifier) {
21 return true;
22 }
23
24 let Notifier = GetConnectionNotify(SideCarIdentifier);
25
26 let Result = tokio::time::timeout(Duration::from_millis(BudgetMilliseconds), Notifier.notified()).await;
27
28 if Result.is_err() {
29 // Budget expired - do a final check in case the connection landed
30 // in the window between notified() registering and the timeout.
31 return IsClientConnected::Fn(SideCarIdentifier);
32 }
33
34 // Woken by FireConnectionNotify - the client is now in the pool.
35 true
36}