Skip to main content

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 and the
8//! crate is built with the `multiplexer` cargo feature, we also open the
9//! bidirectional streaming multiplexer alongside the unary client;
10//! failures there are logged and tolerated.
11
12use std::time::Duration;
13
14use crate::{
15	Client::Shared::{CocoonClient, SIDECAR_CLIENTS},
16	Error::VineError,
17	dev_log,
18};
19
20pub async fn Fn(SideCarIdentifier:&str, Endpoint:&str) -> Result<(), VineError> {
21	let EndpointURL = if Endpoint.starts_with("http://") || Endpoint.starts_with("https://") {
22		Endpoint.to_string()
23	} else {
24		format!("http://{}", Endpoint)
25	};
26
27	let UseTuned = std::env::var("LAND_TONIC_TUNED").as_deref() != Ok("0");
28
29	let mut Channel = tonic::transport::Channel::from_shared(EndpointURL)
30		.map_err(|E| VineError::RPCError(format!("Failed to create channel: {}", E)))?;
31
32	if UseTuned {
33		Channel = Channel
34			.tcp_nodelay(true)
35			.http2_keep_alive_interval(Duration::from_secs(10))
36			.keep_alive_timeout(Duration::from_secs(20))
37			.http2_adaptive_window(true)
38			.initial_stream_window_size(4 * 1024 * 1024)
39			.initial_connection_window_size(16 * 1024 * 1024)
40			.concurrency_limit(1024)
41			.buffer_size(256 * 1024)
42			.timeout(Duration::from_secs(30))
43			.connect_timeout(Duration::from_secs(5));
44	}
45
46	let Connected = Channel
47		.connect()
48		.await
49		.map_err(|E| VineError::RPCError(format!("Failed to connect: {}", E)))?;
50
51	let Client = CocoonClient::new(Connected);
52
53	{
54		let mut Pool = SIDECAR_CLIENTS.lock();
55
56		Pool.insert(SideCarIdentifier.to_string(), Client.clone());
57	}
58
59	#[cfg(feature = "multiplexer")]
60	{
61		if std::env::var("LAND_VINE_STREAMING").as_deref() == Ok("1") {
62			let SideCarForMux = SideCarIdentifier.to_string();
63
64			match crate::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
85	#[cfg(not(feature = "multiplexer"))]
86	{
87		if std::env::var("LAND_VINE_STREAMING").as_deref() == Ok("1") {
88			dev_log!(
89				"grpc",
90				"[VineClient] LAND_VINE_STREAMING=1 set but the `multiplexer` cargo feature is disabled; using unary"
91			);
92		}
93
94		// Keep Client used so the compiler doesn't warn under
95		// --no-default-features.
96		let _ = &Client;
97	}
98
99	Ok(())
100}