a2a_protocol_server/dispatch/
mod.rs1#[cfg(feature = "axum")]
9pub mod axum_adapter;
10pub mod cors;
11#[cfg(feature = "grpc")]
12pub mod grpc;
13pub mod jsonrpc;
14pub mod rest;
15#[cfg(feature = "websocket")]
16pub mod websocket;
17
18pub use cors::CorsConfig;
19#[cfg(feature = "grpc")]
20pub use grpc::{GrpcConfig, GrpcDispatcher};
21pub use jsonrpc::JsonRpcDispatcher;
22pub use rest::RestDispatcher;
23#[cfg(feature = "websocket")]
24pub use websocket::WebSocketDispatcher;
25
26#[derive(Debug, Clone)]
42pub struct DispatchConfig {
43 pub max_request_body_size: usize,
45 pub body_read_timeout: std::time::Duration,
47 pub max_query_string_length: usize,
49 pub sse_keep_alive_interval: std::time::Duration,
54 pub sse_channel_capacity: usize,
59 pub max_batch_size: usize,
64}
65
66impl Default for DispatchConfig {
67 fn default() -> Self {
68 Self {
69 max_request_body_size: 4 * 1024 * 1024,
70 body_read_timeout: std::time::Duration::from_secs(30),
71 max_query_string_length: 4096,
72 sse_keep_alive_interval: std::time::Duration::from_secs(30),
73 sse_channel_capacity: 64,
74 max_batch_size: 100,
75 }
76 }
77}
78
79impl DispatchConfig {
80 #[must_use]
82 pub const fn with_max_request_body_size(mut self, size: usize) -> Self {
83 self.max_request_body_size = size;
84 self
85 }
86
87 #[must_use]
89 pub const fn with_body_read_timeout(mut self, timeout: std::time::Duration) -> Self {
90 self.body_read_timeout = timeout;
91 self
92 }
93
94 #[must_use]
96 pub const fn with_max_query_string_length(mut self, length: usize) -> Self {
97 self.max_query_string_length = length;
98 self
99 }
100
101 #[must_use]
103 pub const fn with_sse_keep_alive_interval(mut self, interval: std::time::Duration) -> Self {
104 self.sse_keep_alive_interval = interval;
105 self
106 }
107
108 #[must_use]
110 pub const fn with_sse_channel_capacity(mut self, capacity: usize) -> Self {
111 self.sse_channel_capacity = capacity;
112 self
113 }
114
115 #[must_use]
117 pub const fn with_max_batch_size(mut self, size: usize) -> Self {
118 self.max_batch_size = size;
119 self
120 }
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126 use std::time::Duration;
127
128 #[test]
129 fn default_values() {
130 let config = DispatchConfig::default();
131 assert_eq!(config.max_request_body_size, 4 * 1024 * 1024);
132 assert_eq!(config.body_read_timeout, Duration::from_secs(30));
133 assert_eq!(config.max_query_string_length, 4096);
134 assert_eq!(config.sse_keep_alive_interval, Duration::from_secs(30));
135 assert_eq!(config.sse_channel_capacity, 64);
136 assert_eq!(config.max_batch_size, 100);
137 }
138
139 #[test]
140 fn with_max_request_body_size_sets_value() {
141 let config = DispatchConfig::default().with_max_request_body_size(8 * 1024 * 1024);
142 assert_eq!(config.max_request_body_size, 8 * 1024 * 1024);
143 }
144
145 #[test]
146 fn with_body_read_timeout_sets_value() {
147 let config = DispatchConfig::default().with_body_read_timeout(Duration::from_secs(60));
148 assert_eq!(config.body_read_timeout, Duration::from_secs(60));
149 }
150
151 #[test]
152 fn with_max_query_string_length_sets_value() {
153 let config = DispatchConfig::default().with_max_query_string_length(8192);
154 assert_eq!(config.max_query_string_length, 8192);
155 }
156
157 #[test]
158 fn with_sse_keep_alive_interval_sets_value() {
159 let config =
160 DispatchConfig::default().with_sse_keep_alive_interval(Duration::from_secs(15));
161 assert_eq!(config.sse_keep_alive_interval, Duration::from_secs(15));
162 }
163
164 #[test]
165 fn with_sse_channel_capacity_sets_value() {
166 let config = DispatchConfig::default().with_sse_channel_capacity(128);
167 assert_eq!(config.sse_channel_capacity, 128);
168 }
169
170 #[test]
171 fn with_max_batch_size_sets_value() {
172 let config = DispatchConfig::default().with_max_batch_size(50);
173 assert_eq!(config.max_batch_size, 50);
174 }
175
176 #[test]
177 fn builder_chaining() {
178 let config = DispatchConfig::default()
179 .with_max_request_body_size(1024)
180 .with_body_read_timeout(Duration::from_secs(10))
181 .with_max_query_string_length(2048)
182 .with_sse_keep_alive_interval(Duration::from_secs(5))
183 .with_sse_channel_capacity(32)
184 .with_max_batch_size(25);
185
186 assert_eq!(config.max_request_body_size, 1024);
187 assert_eq!(config.body_read_timeout, Duration::from_secs(10));
188 assert_eq!(config.max_query_string_length, 2048);
189 assert_eq!(config.sse_keep_alive_interval, Duration::from_secs(5));
190 assert_eq!(config.sse_channel_capacity, 32);
191 assert_eq!(config.max_batch_size, 25);
192 }
193
194 #[test]
195 fn debug_format() {
196 let config = DispatchConfig::default();
197 let debug = format!("{config:?}");
198 assert!(debug.contains("DispatchConfig"));
199 assert!(debug.contains("max_request_body_size"));
200 assert!(debug.contains("body_read_timeout"));
201 assert!(debug.contains("max_query_string_length"));
202 assert!(debug.contains("sse_keep_alive_interval"));
203 assert!(debug.contains("sse_channel_capacity"));
204 assert!(debug.contains("max_batch_size"));
205 }
206}