Skip to main content

spin_factor_outbound_http/runtime_config/
spin.rs

1use serde::Deserialize;
2use spin_factors::runtime_config::toml::GetTomlValue;
3
4/// Get the runtime configuration for outbound HTTP from a TOML table.
5///
6/// Expects table to be in the format:
7/// ```toml
8/// [outbound_http]
9/// connection_pooling = true # optional, defaults to true
10/// max_connections = 10      # optional, defaults to unlimited; 0 = no connections allowed
11/// # max_concurrent_requests is deprecated, use max_connections instead
12/// ```
13pub fn config_from_table(
14    table: &impl GetTomlValue,
15) -> anyhow::Result<Option<super::RuntimeConfig>> {
16    if let Some(outbound_http) = table.get("outbound_http") {
17        let toml = outbound_http.clone().try_into::<OutboundHttpToml>()?;
18
19        let max_connections = match (toml.max_connections, toml.max_concurrent_requests) {
20            (Some(_), Some(_)) => anyhow::bail!(
21                "cannot set both `max_connections` and `max_concurrent_requests` in \
22                 `[outbound_http]`; use `max_connections` only"
23            ),
24            (Some(n), None) => Some(n),
25            (None, Some(n)) => {
26                terminal::warn!(
27                    "`max_concurrent_requests` in `[outbound_http]` is deprecated; \
28                     use `max_connections` instead (note: `max_connections = 0` blocks all \
29                     connections, whereas `max_concurrent_requests = 0` allowed 1 connection)"
30                );
31                // Preserve old semaphore semantics: n+1 permits so that 0 allowed 1 connection
32                Some(n + 1)
33            }
34            (None, None) => None,
35        };
36
37        Ok(Some(super::RuntimeConfig {
38            connection_pooling_enabled: toml.connection_pooling,
39            max_concurrent_connections: max_connections,
40            wait_timeout: None,
41        }))
42    } else {
43        Ok(None)
44    }
45}
46
47#[derive(Debug, Default, Deserialize)]
48#[serde(deny_unknown_fields)]
49struct OutboundHttpToml {
50    #[serde(default)]
51    connection_pooling: bool,
52    #[serde(default)]
53    max_connections: Option<usize>,
54    /// Deprecated. Use `max_connections` instead.
55    #[serde(default)]
56    max_concurrent_requests: Option<usize>,
57}