leyline/core/config/
timeout.rs1use std::time::Duration;
2
3const DEFAULT_TOTAL: Option<Duration> = Some(Duration::from_secs(300));
4const DEFAULT_CONNECT: Option<Duration> = Some(Duration::from_secs(10));
5
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
7#[non_exhaustive]
8pub struct TimeoutConfig {
9 total: Option<Option<Duration>>,
10 connect: Option<Option<Duration>>,
11 read: Option<Option<Duration>>,
12 response_header: Option<Option<Duration>>,
13}
14
15impl TimeoutConfig {
16 pub fn new() -> Self {
17 Self::default()
18 }
19
20 pub fn total(mut self, d: impl Into<Option<Duration>>) -> Self {
21 self.total = Some(d.into());
22 self
23 }
24
25 pub fn connect(mut self, d: impl Into<Option<Duration>>) -> Self {
26 self.connect = Some(d.into());
27 self
28 }
29
30 pub fn read(mut self, d: impl Into<Option<Duration>>) -> Self {
31 self.read = Some(d.into());
32 self
33 }
34
35 pub fn response_header(mut self, d: impl Into<Option<Duration>>) -> Self {
36 self.response_header = Some(d.into());
37 self
38 }
39
40 pub(crate) fn over(self, base: &TimeoutConfig) -> TimeoutConfig {
41 TimeoutConfig {
42 total: self.total.or(base.total),
43 connect: self.connect.or(base.connect),
44 read: self.read.or(base.read),
45 response_header: self.response_header.or(base.response_header),
46 }
47 }
48
49 pub(crate) fn total_limit(&self) -> Option<Duration> {
50 self.total.unwrap_or(DEFAULT_TOTAL)
51 }
52
53 pub(crate) fn connect_limit(&self) -> Option<Duration> {
54 self.connect.unwrap_or(DEFAULT_CONNECT)
55 }
56
57 pub(crate) fn read_limit(&self) -> Option<Duration> {
58 self.read.flatten()
59 }
60
61 pub(crate) fn response_header_limit(&self) -> Option<Duration> {
62 self.response_header.flatten()
63 }
64}
65
66impl From<Duration> for TimeoutConfig {
67 fn from(total: Duration) -> Self {
68 Self::new().total(total)
69 }
70}