Skip to main content

feldera_types/transport/
pubsub.rs

1use serde::{Deserialize, Serialize};
2use utoipa::ToSchema;
3
4use crate::{
5    duration::{ConfigDuration, LegacyUnit},
6    duration_setting,
7};
8
9duration_setting!(duration_timeout, "timeout", LegacyUnit::Secs);
10duration_setting!(
11    duration_connect_timeout,
12    "connect_timeout",
13    LegacyUnit::Secs
14);
15
16// Subscription options docs: https://cloud.google.com/pubsub/docs/subscription-properties
17
18/// Google Pub/Sub input connector configuration.
19#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
20pub struct PubSubInputConfig {
21    /// Set in order to use a Pub/Sub [emulator](https://cloud.google.com/pubsub/docs/emulator)
22    /// instead of the production service, e.g., 'localhost:8681'.
23    pub emulator: Option<String>,
24
25    /// The content of a Google Cloud credentials JSON file.
26    ///
27    /// When this option is specified, the connector will use the provided credentials for
28    /// authentication.  Otherwise, it will use Application Default Credentials (ADC) configured
29    /// in the environment where the Feldera service is running.  See
30    /// [Google Cloud documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc)
31    /// for information on configuring application default credentials.
32    ///
33    /// When running Feldera in an environment where ADC are not configured,
34    /// e.g., a Docker container, use this option to ship Google Cloud credentials from another environment.
35    /// For example, if you use the
36    /// [`gcloud auth application-default login`](https://cloud.google.com/pubsub/docs/authentication#client-libs)
37    /// command for authentication in your local development environment, ADC are stored in the
38    /// `.config/gcloud/application_default_credentials.json` file in your home directory.
39    pub credentials: Option<String>,
40
41    /// Override the default service endpoint 'pubsub.googleapis.com'
42    pub endpoint: Option<String>,
43
44    /// gRPC channel pool size.
45    pub pool_size: Option<u32>,
46
47    /// gRPC request timeout, for example `30s`.
48    #[serde(
49        default,
50        alias = "timeout_seconds",
51        deserialize_with = "duration_timeout",
52        skip_serializing_if = "Option::is_none"
53    )]
54    #[schema(value_type = Option<String>)]
55    pub timeout: Option<ConfigDuration>,
56
57    /// gRPC connection timeout, for example `10s`.
58    #[serde(
59        default,
60        alias = "connect_timeout_seconds",
61        deserialize_with = "duration_connect_timeout",
62        skip_serializing_if = "Option::is_none"
63    )]
64    #[schema(value_type = Option<String>)]
65    pub connect_timeout: Option<ConfigDuration>,
66
67    /// Google Cloud project_id.
68    ///
69    /// When not specified, the connector will use the project id associated
70    /// with the authenticated account.
71    pub project_id: Option<String>,
72
73    /// Subscription name.
74    pub subscription: String,
75
76    /// Reset subscription's backlog to a given snapshot on startup,
77    /// using the Pub/Sub `Seek` API.
78    ///
79    /// This option is mutually exclusive with the `timestamp` option.
80    pub snapshot: Option<String>,
81
82    /// Reset subscription's backlog to a given timestamp on startup,
83    /// using the Pub/Sub `Seek` API.
84    ///
85    /// The value of this option is an ISO 8601-encoded UTC time, e.g., "2024-08-17T16:39:57-08:00".
86    ///
87    /// This option is mutually exclusive with the `snapshot` option.
88    pub timestamp: Option<String>,
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    /// Wraps the fields under test in the `subscription` key the config
96    /// requires.
97    fn config_json(fields: &str) -> String {
98        let separator = if fields.is_empty() { "" } else { ", " };
99        format!(r#"{{"subscription": "test_subscription"{separator}{fields}}}"#)
100    }
101
102    /// The gRPC request timeout has no default of its own: with neither field
103    /// set the accessor yields `None`, which leaves the Pub/Sub client's own
104    /// timeout in place.  Both spellings reach one field, so writing both is
105    /// rejected as a duplicate.
106    #[test]
107    fn timeout_accepts_both_spellings() {
108        for (fields, expected) in [
109            ("", None),
110            (
111                r#""timeout": "750ms""#,
112                Some(ConfigDuration::from_millis(750)),
113            ),
114            (
115                r#""timeout_seconds": 20"#,
116                Some(ConfigDuration::from_secs(20)),
117            ),
118        ] {
119            let json = config_json(fields);
120            let config: PubSubInputConfig = serde_json::from_str(&json).unwrap();
121            assert_eq!(config.timeout, expected, "parsing {json}");
122        }
123    }
124
125    /// See [`timeout_accepts_both_spellings`].
126    #[test]
127    fn connect_timeout_accepts_both_spellings() {
128        for (fields, expected) in [
129            ("", None),
130            (
131                r#""connect_timeout": "5s""#,
132                Some(ConfigDuration::from_secs(5)),
133            ),
134            (
135                r#""connect_timeout_seconds": 30"#,
136                Some(ConfigDuration::from_secs(30)),
137            ),
138        ] {
139            let json = config_json(fields);
140            let config: PubSubInputConfig = serde_json::from_str(&json).unwrap();
141            assert_eq!(config.connect_timeout, expected, "parsing {json}");
142        }
143    }
144}