Skip to main content

Mountain/Vine/Client/
TryConnectSingle.rs

1
2//! Single connection attempt without retry logic. Tunes h2 transport
3//! windows for loopback-to-Cocoon traffic (4 MB stream / 16 MB connection)
4//! so a single rust-analyzer diagnostic emit (200-500 KB) doesn't cause
5//! `WINDOW_UPDATE` ping-pong.
6//!
7//! On success stores the connected `CocoonClient` in
8//! `Shared::SIDECAR_CLIENTS`. If `LAND_VINE_STREAMING=1` is set we also
9//! open the bidirectional streaming multiplexer alongside the unary
10//! client; failures there are logged and tolerated (Cocoon's streaming
11//! handler tree is still on its way).
12
13use std::time::Duration;
14
15use crate::{
16	Vine::{
17		Client::Shared::{CocoonClient, SIDECAR_CLIENTS},
18		Error::VineError,
19	},
20	dev_log,
21};
22
23pub async fn Fn(SideCarIdentifier:&str, Endpoint:&str) -> Result<(), VineError> {
24	let EndpointURL = if Endpoint.starts_with("http://") || Endpoint.starts_with("https://") {
25		Endpoint.to_string()
26	} else {
27		format!("http://{}", Endpoint)
28	};
29
30	let UseTuned = std::env::var("LAND_TONIC_TUNED").as_deref() != Ok("0");
31
32	let mut Channel = tonic::transport::Channel::from_shared(EndpointURL)
33		.map_err(|E| VineError::RPCError(format!("Failed to create channel: {}", E)))?;
34
35	if UseTuned {
36		Channel = Channel
37			.tcp_nodelay(true)
38			.http2_keep_alive_interval(Duration::from_secs(10))
39			.keep_alive_timeout(Duration::from_secs(20))
40			.http2_adaptive_window(true)
41			.initial_stream_window_size(4 * 1024 * 1024)
42			.initial_connection_window_size(16 * 1024 * 1024)
43			.concurrency_limit(1024)
44			.buffer_size(256 * 1024)
45			.timeout(Duration::from_secs(30))
46			.connect_timeout(Duration::from_secs(5));
47	}
48
49	let Connected = Channel
50		.connect()
51		.await
52		.map_err(|E| VineError::RPCError(format!("Failed to connect: {}", E)))?;
53
54	let Client = CocoonClient::new(Connected);
55
56	{
57		let mut Pool = SIDECAR_CLIENTS.lock();
58
59		Pool.insert(SideCarIdentifier.to_string(), Client.clone());
60	}
61
62	if std::env::var("LAND_VINE_STREAMING").as_deref() == Ok("1") {
63		let SideCarForMux = SideCarIdentifier.to_string();
64
65		match crate::Vine::Multiplexer::Multiplexer::Open(SideCarForMux, Client).await {
66			Ok(_) => {
67				dev_log!(
68					"grpc",
69					"[VineClient] streaming multiplexer opened for sidecar '{}'",
70					SideCarIdentifier
71				);
72			},
73
74			Err(Error) => {
75				dev_log!(
76					"grpc",
77					"warn: [VineClient] streaming multiplexer open failed for '{}' ({}); falling back to unary",
78					SideCarIdentifier,
79					Error
80				);
81			},
82		}
83	}
84
85	Ok(())
86}