1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::collections::HashMap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::config::primitives::http_header::HttpHeaderName;
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
#[serde(deny_unknown_fields)]
pub struct DemandControlConfig {
/// Enable demand control processing. Must be `true` for any cost estimation,
/// enforcement or telemetry to take effect.
pub enabled: bool,
/// Configuration for operation cost limits.
///
/// This controls the maximum cost allowed for a single operation executed against the Router, based on the estimated value.
/// When the estimated cost exceeds this value, the request is rejected before any subgraph is contacted.
pub operation_cost: OperationCostConfig,
/// The default list size to use when `@listSize` is not specified in the schema.
#[serde(default = "DefaultListSizeConfig::default")]
pub default_list_size: DefaultListSizeConfig,
/// Subgraph cost limit configuration, including the mode to use for subgraph budget enforcement.
pub subgraphs_budget: SubgraphsBudgetConfig,
/// How actual cost is computed after execution.
///
/// - `by_subgraph` (default): sum the cost computed per individual subgraph
/// fetch responses.
/// - `by_response_shape`: walk the merged supergraph response and reapply
/// the static cost rules. Does not account for intermediate subgraph
/// work.
///
/// Note: the "actual" value calculated in any mode is not used for enforcment.
#[serde(default)]
pub actual_cost_mode: DemandControlActualCostMode,
}
/// Default header name used to expose the configured `max` cost limit when
/// `max` is set to `true`.
const DEFAULT_MAX_HEADER_NAME: &str = "X-Cost-Max";
/// Default header name used to expose the estimated cost when `estimated` is
/// set to `true`.
const DEFAULT_ESTIMATED_HEADER_NAME: &str = "X-Cost-Estimated";
/// Default header name used to expose the actual cost when `actual` is set to
/// `true`.
const DEFAULT_ACTUAL_HEADER_NAME: &str = "X-Cost-Actual";
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq, Eq)]
pub struct OperationCostConfig {
/// The maximum cost allowed for a single operation, based on the estimated value.
///
/// When the estimated cost exceeds this value, the request is rejected before any subgraph is contacted.
pub max: u64,
// Controls what happens when a cost estimation limit is exceeded.
///
/// - `enforce`: reject the incoming request when a limit is breached.
/// - `measure`: never reject. Cost is still computed, result codes are
/// recorded in telemetry (trace, logs, metrics), but no request is
/// blocked. Useful for shadowing a limit in production before switching
/// to `enforce`.
pub mode: DemandControlMode,
/// The headers to expose in the response.
/// Headers are exposed in the response, in both cases when the request is rejected or when it is allowed to proceed.
///
/// Defaults to none.
#[serde(default = "DemandControlExposeHeadersConfig::default")]
pub expose_headers: DemandControlExposeHeadersConfig,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[derive(Default)]
pub struct DemandControlExposeHeadersConfig {
#[serde(default, deserialize_with = "deserialize_max_header")]
pub max: Option<HttpHeaderName>,
#[serde(default, deserialize_with = "deserialize_estimated_header")]
pub estimated: Option<HttpHeaderName>,
#[serde(default, deserialize_with = "deserialize_actual_header")]
pub actual: Option<HttpHeaderName>,
}
#[derive(Deserialize)]
#[serde(untagged)]
enum HeaderNameOrBool {
Bool(bool),
Name(String),
}
/// Deserializes an expose-header field that may be set to a boolean or an
/// explicit header name:
///
/// - `true` -> `Some(default_header_name)`
/// - `false` -> `None`
/// - `"X-My-Header"` -> `Some("X-My-Header")`
///
fn deserialize_header_name_or_bool<'de, D>(
deserializer: D,
default_header_name: &'static str,
) -> Result<Option<HttpHeaderName>, D::Error>
where
D: serde::Deserializer<'de>,
{
match HeaderNameOrBool::deserialize(deserializer)? {
HeaderNameOrBool::Bool(true) => Ok(Some(
HttpHeaderName::new(default_header_name).map_err(serde::de::Error::custom)?,
)),
HeaderNameOrBool::Bool(false) => Ok(None),
HeaderNameOrBool::Name(name) => Ok(Some(
HttpHeaderName::new(name).map_err(serde::de::Error::custom)?,
)),
}
}
fn deserialize_max_header<'de, D>(deserializer: D) -> Result<Option<HttpHeaderName>, D::Error>
where
D: serde::Deserializer<'de>,
{
deserialize_header_name_or_bool(deserializer, DEFAULT_MAX_HEADER_NAME)
}
fn deserialize_estimated_header<'de, D>(deserializer: D) -> Result<Option<HttpHeaderName>, D::Error>
where
D: serde::Deserializer<'de>,
{
deserialize_header_name_or_bool(deserializer, DEFAULT_ESTIMATED_HEADER_NAME)
}
fn deserialize_actual_header<'de, D>(deserializer: D) -> Result<Option<HttpHeaderName>, D::Error>
where
D: serde::Deserializer<'de>,
{
deserialize_header_name_or_bool(deserializer, DEFAULT_ACTUAL_HEADER_NAME)
}
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum DemandControlMode {
Enforce,
Measure,
}
#[derive(Default, Debug, Deserialize, Serialize, JsonSchema, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum DemandControlActualCostMode {
/// Computes the cost of each subgraph response and sums them to get the
/// total query cost. This is the default and preferred mode.
#[default]
BySubgraph,
/// Computes the cost based on the final shape of the merged response.
/// Does not account for intermediate subgraph work. This was the only
/// option prior to `by_subgraph`.
ByResponseShape,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
#[serde(deny_unknown_fields)]
pub struct SubgraphsBudgetConfig {
/// The mode to use for subgraph budget enforcement.
///
/// In `mode: enforce`, when a subgraph limit is exceeded:
/// - The router **continues** executing the rest of the query plan.
/// - The specific subgraph fetch is skipped and a `SUBGRAPH_COST_ESTIMATED_TOO_EXPENSIVE` error is added to the response.
/// - The fetch call assumes error, and returns `null` as subgraph response.
///
/// This kind of enforcement is applied to each subgraph fetch individually, during execution,
/// in order to prevent false-positives from exceeding the limit.
///
/// In `mode: measure`, subgraph limits are never enforced.
pub mode: DemandControlMode,
/// Default limit configuration applied to every subgraph unless overridden.
pub all: Option<usize>,
/// Per-subgraph overrides. Keys are subgraph names.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub subgraphs: Option<HashMap<String, usize>>,
}
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
#[serde(deny_unknown_fields)]
#[derive(Default)]
pub struct DefaultListSizeConfig {
/// Default list size for fields in the supergraph that have no `@listSize` directive.
pub all: Option<usize>,
/// Per-subgraph overrides. Keys are subgraph names.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub subgraphs: Option<HashMap<String, usize>>,
}