1pub mod gRPCTransport;
34pub mod IPCTransport;
35pub mod Strategy;
36pub mod WASMTransport;
37
38use 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
47pub const DEFAULT_CONNECTION_TIMEOUT_MS:u64 = 5000;
49
50pub const DEFAULT_REQUEST_TIMEOUT_MS:u64 = 30000;
52
53#[derive(Debug, Clone)]
55pub struct TransportConfig {
56 pub connection_timeout:Duration,
58 pub request_timeout:Duration,
60 pub max_retries:u32,
62 pub retry_delay:Duration,
64 pub keepalive_enabled:bool,
66 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 pub fn new() -> Self { Self::default() }
86
87 pub fn with_connection_timeout(mut self, timeout:Duration) -> Self {
89 self.connection_timeout = timeout;
90 self
91 }
92
93 pub fn with_request_timeout(mut self, timeout:Duration) -> Self {
95 self.request_timeout = timeout;
96 self
97 }
98
99 pub fn with_max_retries(mut self, max_retries:u32) -> Self {
101 self.max_retries = max_retries;
102 self
103 }
104
105 pub fn with_retry_delay(mut self, delay:Duration) -> Self {
107 self.retry_delay = delay;
108 self
109 }
110
111 pub fn with_keepalive(mut self, enabled:bool) -> Self {
113 self.keepalive_enabled = enabled;
114 self
115 }
116}
117
118pub fn create_default_transport() -> Transport { Transport::default() }
120
121pub fn create_grpc_transport(address:&str) -> Result<Transport> { Ok(Transport::gRPC(GrpcTransport::new(address)?)) }
123
124pub fn create_ipc_transport() -> Result<Transport> { Ok(Transport::IPC(IPCTransportImpl::new()?)) }
126
127pub 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 match transport {
161 Transport::gRPC(_) | Transport::IPC(_) | Transport::WASM(_) => {},
162 }
163 }
164}