-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into geal/clean-up-dedup-on-cancel
- Loading branch information
Showing
38 changed files
with
499 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod forbid_mutations; | ||
mod headers; | ||
mod traffic_shaping; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use std::collections::HashMap; | ||
|
||
use schemars::JsonSchema; | ||
use serde::Deserialize; | ||
use tower::util::BoxService; | ||
use tower::{BoxError, ServiceBuilder, ServiceExt}; | ||
|
||
use crate::deduplication::QueryDeduplicationLayer; | ||
use crate::plugin::Plugin; | ||
use crate::{register_plugin, SubgraphRequest, SubgraphResponse}; | ||
|
||
#[derive(PartialEq, Debug, Clone, Deserialize, JsonSchema)] | ||
struct Shaping { | ||
dedup: Option<bool>, | ||
} | ||
|
||
impl Shaping { | ||
fn merge(&self, fallback: Option<&Shaping>) -> Shaping { | ||
match fallback { | ||
None => self.clone(), | ||
Some(fallback) => Shaping { | ||
dedup: self.dedup.or(fallback.dedup), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Debug, Clone, Deserialize, JsonSchema)] | ||
struct Config { | ||
#[serde(default)] | ||
all: Option<Shaping>, | ||
#[serde(default)] | ||
subgraphs: HashMap<String, Shaping>, | ||
} | ||
|
||
struct TrafficShaping { | ||
config: Config, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Plugin for TrafficShaping { | ||
type Config = Config; | ||
|
||
fn new(config: Self::Config) -> Result<Self, BoxError> { | ||
Ok(Self { config }) | ||
} | ||
|
||
fn subgraph_service( | ||
&mut self, | ||
name: &str, | ||
service: BoxService<SubgraphRequest, SubgraphResponse, BoxError>, | ||
) -> BoxService<SubgraphRequest, SubgraphResponse, BoxError> { | ||
// Either we have the subgraph config and we merge it with the all config, or we just have the all config or we have nothing. | ||
let all_config = self.config.all.as_ref(); | ||
let subgraph_config = self.config.subgraphs.get(name); | ||
let final_config = Self::merge_config(all_config, subgraph_config); | ||
|
||
if let Some(config) = final_config { | ||
ServiceBuilder::new() | ||
.option_layer(config.dedup.unwrap_or_default().then(|| { | ||
//Buffer is required because dedup layer requires a clone service. | ||
ServiceBuilder::new() | ||
.layer(QueryDeduplicationLayer::default()) | ||
.buffer(20_000) | ||
})) | ||
.service(service) | ||
.boxed() | ||
} else { | ||
service | ||
} | ||
} | ||
} | ||
|
||
impl TrafficShaping { | ||
fn merge_config( | ||
all_config: Option<&Shaping>, | ||
subgraph_config: Option<&Shaping>, | ||
) -> Option<Shaping> { | ||
let merged_subgraph_config = subgraph_config.map(|c| c.merge(all_config)); | ||
merged_subgraph_config.or_else(|| all_config.cloned()) | ||
} | ||
} | ||
|
||
register_plugin!("experimental", "traffic_shaping", TrafficShaping); | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_merge_config() { | ||
let config = serde_yaml::from_str::<Config>( | ||
r#" | ||
all: | ||
dedup: true | ||
subgraphs: | ||
products: | ||
dedup: false | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(TrafficShaping::merge_config(None, None), None); | ||
assert_eq!( | ||
TrafficShaping::merge_config(config.all.as_ref(), None), | ||
config.all | ||
); | ||
assert_eq!( | ||
TrafficShaping::merge_config(config.all.as_ref(), config.subgraphs.get("products")) | ||
.as_ref(), | ||
config.subgraphs.get("products") | ||
); | ||
|
||
assert_eq!( | ||
TrafficShaping::merge_config(None, config.subgraphs.get("products")).as_ref(), | ||
config.subgraphs.get("products") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.