Skip to main content

Mountain/Vine/Client/
WaitForClientConnection.rs

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