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
33	let mut Channel = tonic::transport::Channel::from_shared(EndpointURL)
34		.map_err(|E| VineError::RPCError(format!("Failed to create channel: {}", E)))?;
35
36	if UseTuned {
37		Channel = Channel
38			.tcp_nodelay(true)
39			.http2_keep_alive_interval(Duration::from_secs(10))
40			.keep_alive_timeout(Duration::from_secs(20))
41			.http2_adaptive_window(true)
42			.initial_stream_window_size(4 * 1024 * 1024)
43			.initial_connection_window_size(16 * 1024 * 1024)
44			.concurrency_limit(1024)
45			.buffer_size(256 * 1024)
46			.timeout(Duration::from_secs(30))
47			.connect_timeout(Duration::from_secs(5));
48	}
49
50	let Connected = Channel
51		.connect()
52		.await
53		.map_err(|E| VineError::RPCError(format!("Failed to connect: {}", E)))?;
54
55	let Client = CocoonClient::new(Connected);
56
57	{
58		let mut Pool = SIDECAR_CLIENTS.lock();
59
60		Pool.insert(SideCarIdentifier.to_string(), Client.clone());
61	}
62
63	if std::env::var("LAND_VINE_STREAMING").as_deref() == Ok("1") {
64		let SideCarForMux = SideCarIdentifier.to_string();
65
66		match crate::Vine::Multiplexer::Multiplexer::Open(SideCarForMux, Client).await {
67			Ok(_) => {
68				dev_log!(
69					"grpc",
70					"[VineClient] streaming multiplexer opened for sidecar '{}'",
71					SideCarIdentifier
72				);
73			},
74
75			Err(Error) => {
76				dev_log!(
77					"grpc",
78					"warn: [VineClient] streaming multiplexer open failed for '{}' ({}); falling back to unary",
79					SideCarIdentifier,
80					Error
81				);
82			},
83		}
84	}
85
86	Ok(())
87}