Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Vine/Client/
TryConnectSingle.rs

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