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