Skip to main content

Mountain/Vine/Client/
TryConnectSingle.rs

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