openmoat_proxy/server/
limits.rs1use std::io::{self, ErrorKind};
4use std::time::{Duration, Instant};
5
6#[derive(Debug, Clone)]
8pub struct Limits {
9 pub max_head_bytes: usize,
11 pub handshake_timeout: Duration,
13 pub idle_timeout: Duration,
15 pub connect_timeout: Duration,
17 pub max_connections: usize,
21 pub taint_recheck: Duration,
25}
26
27impl Limits {
28 pub(super) fn check(&self) -> io::Result<()> {
31 let now = Instant::now();
32 let sizes = [
33 ("max_head_bytes", self.max_head_bytes),
34 ("max_connections", self.max_connections),
35 ];
36 let timeouts = [
37 ("handshake_timeout", self.handshake_timeout),
38 ("idle_timeout", self.idle_timeout),
39 ("connect_timeout", self.connect_timeout),
40 ("taint_recheck", self.taint_recheck),
41 ];
42 let bad = sizes
43 .iter()
44 .find(|(_, n)| *n == 0)
45 .map(|(name, _)| name)
46 .or_else(|| {
47 timeouts
48 .iter()
49 .find(|(_, d)| d.is_zero() || now.checked_add(*d).is_none())
50 .map(|(name, _)| name)
51 });
52 match bad {
53 Some(name) => Err(io::Error::new(
54 ErrorKind::InvalidInput,
55 format!("proxy limit `{name}` is zero or too large"),
56 )),
57 None => Ok(()),
58 }
59 }
60}
61
62impl Default for Limits {
63 fn default() -> Self {
64 Self {
65 max_head_bytes: 16 * 1024,
66 handshake_timeout: Duration::from_secs(10),
67 idle_timeout: Duration::from_mins(2),
68 connect_timeout: Duration::from_secs(10),
69 max_connections: 256,
70 taint_recheck: Duration::from_secs(1),
71 }
72 }
73}