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