Skip to main content

Grove/Transport/
mod.rs

1//! Transport Layer Module
2//!
3//! Provides different communication strategies for Grove.
4//! Supports gRPC, IPC, and WASM-based transport methods.
5//!
6//! # Architecture
7//!
8//! ```text
9//! +++++++++++++++++++++++++++++++++++++++++++
10//! +          Transport Strategy             +
11//! +++++++++++++++++++++++++++++++++++++++++++
12//! +  • gRPC - Network-based communication  +
13//! +  • IPC   - Local process communication +
14//! +  • WASM  - Direct WASM communication   +
15//! +++++++++++++++++++++++++++++++++++++++++++
16//!           +                    +
17//!           ▼                    ▼
18//! ++++++++++++++++++++  ++++++++++++++++++++
19//! + Mountain/Core    +  +  Extension       +
20//! +  (gRPC client)   +  +  Module (WASM)   +
21//! ++++++++++++++++++++  ++++++++++++++++++++
22//! ```
23//!
24//! # Key Components
25//!
26//! - [`Strategy`] - Transport strategy trait
27//! - [`gRPCTransport`] - gRPC-based communication
28//! - [`IPCTransport`] - Inter-process communication
29//! - [`WASMTransport`] - Direct WASM module communication
30
31pub mod gRPCTransport;
32pub mod IPCTransport;
33pub mod Strategy;
34pub mod WASMTransport;
35
36use std::time::Duration;
37
38use anyhow::Result;
39// Types accessed via full paths: Transport::Strategy::Transport, etc.
40
41/// Default connection timeout
42pub const DEFAULT_CONNECTION_TIMEOUT_MS:u64 = 5000;
43
44/// Default request timeout
45pub const DEFAULT_REQUEST_TIMEOUT_MS:u64 = 30000;
46
47/// Transport configuration.
48#[derive(Debug, Clone)]
49pub struct TransportConfig {
50	/// Connection timeout.
51	pub ConnectionTimeout:Duration,
52	/// Request timeout.
53	pub RequestTimeout:Duration,
54	/// Maximum number of retries.
55	pub MaximumRetries:u32,
56	/// Delay between retries.
57	pub RetryDelay:Duration,
58	/// Whether keepalive is enabled.
59	pub KeepaliveEnabled:bool,
60	/// Keepalive interval.
61	pub KeepaliveInterval:Duration,
62}
63
64impl Default for TransportConfig {
65	fn default() -> Self {
66		Self {
67			ConnectionTimeout:Duration::from_millis(DEFAULT_CONNECTION_TIMEOUT_MS),
68			RequestTimeout:Duration::from_millis(DEFAULT_REQUEST_TIMEOUT_MS),
69			MaximumRetries:3,
70			RetryDelay:Duration::from_millis(1000),
71			KeepaliveEnabled:true,
72			KeepaliveInterval:Duration::from_secs(30),
73		}
74	}
75}
76
77impl TransportConfig {
78	/// Creates a new `TransportConfig` with default values.
79	pub fn New() -> Self { Self::default() }
80
81	/// Sets the connection timeout.
82	pub fn WithConnectionTimeout(mut self, Timeout:Duration) -> Self {
83		self.ConnectionTimeout = Timeout;
84		self
85	}
86
87	/// Sets the request timeout.
88	pub fn WithRequestTimeout(mut self, Timeout:Duration) -> Self {
89		self.RequestTimeout = Timeout;
90		self
91	}
92
93	/// Sets the maximum number of retries.
94	pub fn WithMaximumRetries(mut self, MaximumRetries:u32) -> Self {
95		self.MaximumRetries = MaximumRetries;
96		self
97	}
98
99	/// Sets the retry delay.
100	pub fn WithRetryDelay(mut self, Delay:Duration) -> Self {
101		self.RetryDelay = Delay;
102		self
103	}
104
105	/// Enables or disables keepalive.
106	pub fn WithKeepalive(mut self, Enabled:bool) -> Self {
107		self.KeepaliveEnabled = Enabled;
108		self
109	}
110}
111
112/// Creates the default transport (gRPC to localhost).
113pub fn CreateDefaultTransport() -> Strategy::Transport { Strategy::Transport::default() }
114
115/// Creates a gRPC transport connecting to the given address.
116pub fn CreategRPCTransport(Address:&str) -> Result<Strategy::Transport> {
117	Ok(Strategy::Transport::gRPC(gRPCTransport::gRPCTransport::New(Address)?))
118}
119
120/// Creates an IPC transport using the default socket/pipe path.
121pub fn CreateIPCTransport() -> Result<Strategy::Transport> {
122	Ok(Strategy::Transport::IPC(IPCTransport::IPCTransport::New()?))
123}
124
125/// Creates a WASM transport with the given configuration.
126pub fn CreateWASMTransport(
127	EnableWASI:bool,
128	MemoryLimitMegabytes:u64,
129	MaxExecutionTimeMilliseconds:u64,
130) -> Result<Strategy::Transport> {
131	Ok(Strategy::Transport::WASM(WASMTransport::WASMTransportImpl::new(
132		EnableWASI,
133		MemoryLimitMegabytes,
134		MaxExecutionTimeMilliseconds,
135	)?))
136}
137
138#[cfg(test)]
139mod tests {
140	use super::*;
141
142	#[test]
143	fn TestTransportConfigDefault() {
144		let Configuration = TransportConfig::default();
145		assert_eq!(
146			Configuration.ConnectionTimeout.as_millis(),
147			DEFAULT_CONNECTION_TIMEOUT_MS as u128
148		);
149	}
150
151	#[test]
152	fn TestTransportConfigBuilder() {
153		let Configuration = TransportConfig::default()
154			.WithConnectionTimeout(Duration::from_secs(10))
155			.WithMaximumRetries(5);
156
157		assert_eq!(Configuration.ConnectionTimeout.as_secs(), 10);
158		assert_eq!(Configuration.MaximumRetries, 5);
159	}
160
161	#[test]
162	fn TestTransportDefault() {
163		let TransportValue = CreateDefaultTransport();
164		match TransportValue {
165			Strategy::Transport::gRPC(_) | Strategy::Transport::IPC(_) | Strategy::Transport::WASM(_) => {},
166		}
167	}
168}