Skip to main content

feldera_types/transport/
url.rs

1use serde::{Deserialize, Serialize};
2use utoipa::ToSchema;
3
4use crate::{
5    duration::{ConfigDuration, LegacyUnit},
6    duration_setting,
7};
8
9/// Default timeout before a paused input adapter drops its connection.
10pub const DEFAULT_PAUSE_TIMEOUT: ConfigDuration = ConfigDuration::from_secs(60);
11
12/// The default as the OpenAPI schema shows it.
13fn default_pause_linger() -> String {
14    DEFAULT_PAUSE_TIMEOUT.to_string()
15}
16
17duration_setting!(duration_pause_linger, "pause_linger", LegacyUnit::Secs);
18
19/// Configuration for reading data from an HTTP or HTTPS URL with
20/// `UrlInputTransport`.
21#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
22pub struct UrlInputConfig {
23    /// URL.
24    pub path: String,
25
26    /// Timeout before disconnection when paused, for example `60s`.
27    ///
28    /// If the pipeline is paused, or if the input adapter reads data faster
29    /// than the pipeline can process it, then the controller will pause the
30    /// input adapter. If the input adapter stays paused longer than this
31    /// timeout, it will drop the network connection to the server. It will
32    /// automatically reconnect when the input adapter starts running again.
33    ///
34    /// Unset selects the default, `DEFAULT_PAUSE_TIMEOUT`.
35    #[serde(
36        default,
37        alias = "pause_timeout",
38        deserialize_with = "duration_pause_linger",
39        skip_serializing_if = "Option::is_none"
40    )]
41    #[schema(value_type = Option<String>, default = default_pause_linger)]
42    pub pause_linger: Option<ConfigDuration>,
43}
44
45impl UrlInputConfig {
46    /// The timeout in effect, or [`DEFAULT_PAUSE_TIMEOUT`].
47    pub fn pause_linger(&self) -> ConfigDuration {
48        self.pause_linger.unwrap_or(DEFAULT_PAUSE_TIMEOUT)
49    }
50}
51
52impl crate::sensitive::SensitiveValues for UrlInputConfig {
53    fn visit_sensitive_values(&self, visit: &mut dyn FnMut(&str, &str)) {
54        crate::sensitive::visit_url("path", &self.path, visit);
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    fn parse(json: &str) -> UrlInputConfig {
63        serde_json::from_str(json).unwrap()
64    }
65
66    /// The pause timeout reads back from the current spelling and from the one
67    /// it deprecates.
68    #[test]
69    fn pause_linger_accepts_both_spellings() {
70        for (json, expected) in [
71            (r#"{"path": "http://x"}"#, DEFAULT_PAUSE_TIMEOUT),
72            (
73                r#"{"path": "http://x", "pause_linger": "5m"}"#,
74                ConfigDuration::from_secs(300),
75            ),
76            (
77                r#"{"path": "http://x", "pause_timeout": 10}"#,
78                ConfigDuration::from_secs(10),
79            ),
80        ] {
81            assert_eq!(parse(json).pause_linger(), expected, "parsing {json}");
82        }
83    }
84
85    #[test]
86    fn unset_fields_do_not_serialize() {
87        let json = serde_json::to_string(&parse(r#"{"path": "http://x"}"#)).unwrap();
88        assert_eq!(json, r#"{"path":"http://x"}"#);
89    }
90}