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
31
32
33pub mod gRPCTransport;
34pub mod IPCTransport;
35pub mod Strategy;
36pub mod WASMTransport;
37
38// Re-exports for convenience
39use std::time::Duration;
40
41pub use Strategy::{Transport, TransportStrategy, TransportType, TransportStats};
42pub use gRPCTransport::GrpcTransport;
43pub use IPCTransport::IPCTransportImpl;
44pub use WASMTransport::WASMTransportImpl;
45use anyhow::Result;
46
47/// Default connection timeout
48pub const DEFAULT_CONNECTION_TIMEOUT_MS:u64 = 5000;
49
50/// Default request timeout
51pub const DEFAULT_REQUEST_TIMEOUT_MS:u64 = 30000;
52
53/// Transport configuration
54#[derive(Debug, Clone)]
55pub struct TransportConfig {
56	/// Connection timeout
57	pub connection_timeout:Duration,
58	/// Request timeout
59	pub request_timeout:Duration,
60	/// Maximum number of retries
61	pub max_retries:u32,
62	/// Retry delay
63	pub retry_delay:Duration,
64	/// Enable keepalive
65	pub keepalive_enabled:bool,
66	/// Keepalive interval
67	pub keepalive_interval:Duration,
68}
69
70impl Default for TransportConfig {
71	fn default() -> Self {
72		Self {
73			connection_timeout:Duration::from_millis(DEFAULT_CONNECTION_TIMEOUT_MS),
74			request_timeout:Duration::from_millis(DEFAULT_REQUEST_TIMEOUT_MS),
75			max_retries:3,
76			retry_delay:Duration::from_millis(1000),
77			keepalive_enabled:true,
78			keepalive_interval:Duration::from_secs(30),
79		}
80	}
81}
82
83impl TransportConfig {
84	/// Create a new transport configuration
85	pub fn new() -> Self { Self::default() }
86
87	/// Set the connection timeout
88	pub fn with_connection_timeout(mut self, timeout:Duration) -> Self {
89		self.connection_timeout = timeout;
90		self
91	}
92
93	/// Set the request timeout
94	pub fn with_request_timeout(mut self, timeout:Duration) -> Self {
95		self.request_timeout = timeout;
96		self
97	}
98
99	/// Set the maximum number of retries
100	pub fn with_max_retries(mut self, max_retries:u32) -> Self {
101		self.max_retries = max_retries;
102		self
103	}
104
105	/// Set the retry delay
106	pub fn with_retry_delay(mut self, delay:Duration) -> Self {
107		self.retry_delay = delay;
108		self
109	}
110
111	/// Enable or disable keepalive
112	pub fn with_keepalive(mut self, enabled:bool) -> Self {
113		self.keepalive_enabled = enabled;
114		self
115	}
116}
117
118/// Create a default transport
119pub fn create_default_transport() -> Transport { Transport::default() }
120
121/// Create a gRPC transport with the given address
122pub fn create_grpc_transport(address:&str) -> Result<Transport> { Ok(Transport::gRPC(GrpcTransport::new(address)?)) }
123
124/// Create an IPC transport
125pub fn create_ipc_transport() -> Result<Transport> { Ok(Transport::IPC(IPCTransportImpl::new()?)) }
126
127/// Create a WASM transport with the given configuration
128pub fn create_wasm_transport(enable_wasi:bool, memory_limit_mb:u64, max_execution_time_ms:u64) -> Result<Transport> {
129	Ok(Transport::WASM(WASMTransportImpl::new(
130		enable_wasi,
131		memory_limit_mb,
132		max_execution_time_ms,
133	)?))
134}
135
136#[cfg(test)]
137mod tests {
138	use super::*;
139
140	#[test]
141	fn test_transport_config_default() {
142		let config = TransportConfig::default();
143		assert_eq!(config.connection_timeout.as_millis(), DEFAULT_CONNECTION_TIMEOUT_MS as u128);
144	}
145
146	#[test]
147	fn test_transport_config_builder() {
148		let config = TransportConfig::default()
149			.with_connection_timeout(Duration::from_secs(10))
150			.with_max_retries(5);
151
152		assert_eq!(config.connection_timeout.as_secs(), 10);
153		assert_eq!(config.max_retries, 5);
154	}
155
156	#[test]
157	fn test_transport_default() {
158		let transport = create_default_transport();
159		// Just test that it can be created
160		match transport {
161			Transport::gRPC(_) | Transport::IPC(_) | Transport::WASM(_) => {},
162		}
163	}
164}