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;
32
33pub mod IPCTransport;
34
35pub mod Strategy;
36
37pub mod WASMTransport;
38
39use std::time::Duration;
40
41use anyhow::Result;
42
43// Types accessed via full paths: Transport::Strategy::Transport, etc.
44
45/// Default connection timeout
46pub const DEFAULT_CONNECTION_TIMEOUT_MS:u64 = 5000;
47
48/// Default request timeout
49pub const DEFAULT_REQUEST_TIMEOUT_MS:u64 = 30000;
50
51/// Transport configuration.
52#[derive(Debug, Clone)]
53pub struct TransportConfig {
54	/// Connection timeout.
55	pub ConnectionTimeout:Duration,
56
57	/// Request timeout.
58	pub RequestTimeout:Duration,
59
60	/// Maximum number of retries.
61	pub MaximumRetries:u32,
62
63	/// Delay between retries.
64	pub RetryDelay:Duration,
65
66	/// Whether keepalive is enabled.
67	pub KeepaliveEnabled:bool,
68
69	/// Keepalive interval.
70	pub KeepaliveInterval:Duration,
71}
72
73impl Default for TransportConfig {
74	fn default() -> Self {
75		Self {
76			ConnectionTimeout:Duration::from_millis(DEFAULT_CONNECTION_TIMEOUT_MS),
77
78			RequestTimeout:Duration::from_millis(DEFAULT_REQUEST_TIMEOUT_MS),
79
80			MaximumRetries:3,
81
82			RetryDelay:Duration::from_millis(1000),
83
84			KeepaliveEnabled:true,
85
86			KeepaliveInterval:Duration::from_secs(30),
87		}
88	}
89}
90
91impl TransportConfig {
92	/// Creates a new `TransportConfig` with default values.
93	pub fn New() -> Self { Self::default() }
94
95	/// Sets the connection timeout.
96	pub fn WithConnectionTimeout(mut self, Timeout:Duration) -> Self {
97		self.ConnectionTimeout = Timeout;
98
99		self
100	}
101
102	/// Sets the request timeout.
103	pub fn WithRequestTimeout(mut self, Timeout:Duration) -> Self {
104		self.RequestTimeout = Timeout;
105
106		self
107	}
108
109	/// Sets the maximum number of retries.
110	pub fn WithMaximumRetries(mut self, MaximumRetries:u32) -> Self {
111		self.MaximumRetries = MaximumRetries;
112
113		self
114	}
115
116	/// Sets the retry delay.
117	pub fn WithRetryDelay(mut self, Delay:Duration) -> Self {
118		self.RetryDelay = Delay;
119
120		self
121	}
122
123	/// Enables or disables keepalive.
124	pub fn WithKeepalive(mut self, Enabled:bool) -> Self {
125		self.KeepaliveEnabled = Enabled;
126
127		self
128	}
129}
130
131/// Creates the default transport (gRPC to localhost).
132pub fn CreateDefaultTransport() -> Strategy::Transport { Strategy::Transport::default() }
133
134/// Creates a gRPC transport connecting to the given address.
135pub fn CreategRPCTransport(Address:&str) -> Result<Strategy::Transport> {
136	Ok(Strategy::Transport::gRPC(gRPCTransport::gRPCTransport::New(Address)?))
137}
138
139/// Creates an IPC transport using the default socket/pipe path.
140pub fn CreateIPCTransport() -> Result<Strategy::Transport> {
141	Ok(Strategy::Transport::IPC(IPCTransport::IPCTransport::New()?))
142}
143
144/// Creates a WASM transport with the given configuration.
145pub fn CreateWASMTransport(
146	EnableWASI:bool,
147
148	MemoryLimitMegabytes:u64,
149
150	MaxExecutionTimeMilliseconds:u64,
151) -> Result<Strategy::Transport> {
152	Ok(Strategy::Transport::WASM(WASMTransport::WASMTransportImpl::new(
153		EnableWASI,
154		MemoryLimitMegabytes,
155		MaxExecutionTimeMilliseconds,
156	)?))
157}
158
159#[cfg(test)]
160mod tests {
161
162	use super::*;
163
164	#[test]
165	fn TestTransportConfigDefault() {
166		let Configuration = TransportConfig::default();
167
168		assert_eq!(
169			Configuration.ConnectionTimeout.as_millis(),
170			DEFAULT_CONNECTION_TIMEOUT_MS as u128
171		);
172	}
173
174	#[test]
175	fn TestTransportConfigBuilder() {
176		let Configuration = TransportConfig::default()
177			.WithConnectionTimeout(Duration::from_secs(10))
178			.WithMaximumRetries(5);
179
180		assert_eq!(Configuration.ConnectionTimeout.as_secs(), 10);
181
182		assert_eq!(Configuration.MaximumRetries, 5);
183	}
184
185	#[test]
186	fn TestTransportDefault() {
187		let TransportValue = CreateDefaultTransport();
188
189		match TransportValue {
190			Strategy::Transport::gRPC(_) | Strategy::Transport::IPC(_) | Strategy::Transport::WASM(_) => {},
191		}
192	}
193}