a2a_protocol_server/handler/
limits.rs1use std::time::Duration;
9
10#[derive(Debug, Clone)]
25pub struct HandlerLimits {
26 pub max_id_length: usize,
28 pub max_metadata_size: usize,
30 pub max_cancellation_tokens: usize,
32 pub max_token_age: Duration,
34 pub push_delivery_timeout: Duration,
40 pub max_artifacts_per_task: usize,
46 pub max_context_locks: usize,
52}
53
54impl Default for HandlerLimits {
55 fn default() -> Self {
56 Self {
57 max_id_length: 1024,
58 max_metadata_size: 1_048_576,
59 max_cancellation_tokens: 10_000,
60 max_token_age: Duration::from_secs(3600),
61 push_delivery_timeout: Duration::from_secs(5),
62 max_artifacts_per_task: 1000,
63 max_context_locks: 10_000,
64 }
65 }
66}
67
68impl HandlerLimits {
69 #[must_use]
71 pub const fn with_max_id_length(mut self, length: usize) -> Self {
72 self.max_id_length = length;
73 self
74 }
75
76 #[must_use]
78 pub const fn with_max_metadata_size(mut self, size: usize) -> Self {
79 self.max_metadata_size = size;
80 self
81 }
82
83 #[must_use]
85 pub const fn with_max_cancellation_tokens(mut self, max: usize) -> Self {
86 self.max_cancellation_tokens = max;
87 self
88 }
89
90 #[must_use]
92 pub const fn with_max_token_age(mut self, age: Duration) -> Self {
93 self.max_token_age = age;
94 self
95 }
96
97 #[must_use]
99 pub const fn with_push_delivery_timeout(mut self, timeout: Duration) -> Self {
100 self.push_delivery_timeout = timeout;
101 self
102 }
103
104 #[must_use]
106 pub const fn with_max_artifacts_per_task(mut self, max: usize) -> Self {
107 self.max_artifacts_per_task = max;
108 self
109 }
110
111 #[must_use]
113 pub const fn with_max_context_locks(mut self, max: usize) -> Self {
114 self.max_context_locks = max;
115 self
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn default_values() {
125 let limits = HandlerLimits::default();
126 assert_eq!(limits.max_id_length, 1024);
127 assert_eq!(limits.max_metadata_size, 1_048_576);
128 assert_eq!(limits.max_cancellation_tokens, 10_000);
129 assert_eq!(limits.max_token_age, Duration::from_secs(3600));
130 assert_eq!(limits.push_delivery_timeout, Duration::from_secs(5));
131 assert_eq!(limits.max_artifacts_per_task, 1000);
132 assert_eq!(limits.max_context_locks, 10_000);
133 }
134
135 #[test]
136 fn with_max_id_length_sets_value() {
137 let limits = HandlerLimits::default().with_max_id_length(2048);
138 assert_eq!(limits.max_id_length, 2048);
139 }
140
141 #[test]
142 fn with_max_metadata_size_sets_value() {
143 let limits = HandlerLimits::default().with_max_metadata_size(2_097_152);
144 assert_eq!(limits.max_metadata_size, 2_097_152);
145 }
146
147 #[test]
148 fn with_max_cancellation_tokens_sets_value() {
149 let limits = HandlerLimits::default().with_max_cancellation_tokens(5_000);
150 assert_eq!(limits.max_cancellation_tokens, 5_000);
151 }
152
153 #[test]
154 fn with_max_token_age_sets_value() {
155 let limits = HandlerLimits::default().with_max_token_age(Duration::from_secs(7200));
156 assert_eq!(limits.max_token_age, Duration::from_secs(7200));
157 }
158
159 #[test]
160 fn with_push_delivery_timeout_sets_value() {
161 let limits = HandlerLimits::default().with_push_delivery_timeout(Duration::from_secs(10));
162 assert_eq!(limits.push_delivery_timeout, Duration::from_secs(10));
163 }
164
165 #[test]
166 fn builder_chaining() {
167 let limits = HandlerLimits::default()
168 .with_max_id_length(512)
169 .with_max_metadata_size(500_000)
170 .with_max_cancellation_tokens(1_000)
171 .with_max_token_age(Duration::from_secs(1800))
172 .with_push_delivery_timeout(Duration::from_secs(15));
173
174 assert_eq!(limits.max_id_length, 512);
175 assert_eq!(limits.max_metadata_size, 500_000);
176 assert_eq!(limits.max_cancellation_tokens, 1_000);
177 assert_eq!(limits.max_token_age, Duration::from_secs(1800));
178 assert_eq!(limits.push_delivery_timeout, Duration::from_secs(15));
179 }
180
181 #[test]
182 fn with_max_artifacts_per_task_sets_value() {
183 let limits = HandlerLimits::default().with_max_artifacts_per_task(500);
184 assert_eq!(limits.max_artifacts_per_task, 500);
185 }
186
187 #[test]
188 fn debug_format() {
189 let limits = HandlerLimits::default();
190 let debug = format!("{limits:?}");
191 assert!(debug.contains("HandlerLimits"));
192 assert!(debug.contains("max_id_length"));
193 assert!(debug.contains("max_metadata_size"));
194 assert!(debug.contains("max_cancellation_tokens"));
195 assert!(debug.contains("max_token_age"));
196 assert!(debug.contains("push_delivery_timeout"));
197 assert!(debug.contains("max_artifacts_per_task"));
198 assert!(debug.contains("max_context_locks"));
199 }
200}