-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* validate middleware * validate extension for config * rename
- Loading branch information
1 parent
354f917
commit 50b826d
Showing
8 changed files
with
156 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use crate::extensions::client::Client; | ||
use crate::middlewares::{CallRequest, CallResult}; | ||
use crate::utils::errors; | ||
use async_trait::async_trait; | ||
use serde::Deserialize; | ||
use std::sync::Arc; | ||
|
||
use super::{Extension, ExtensionRegistry}; | ||
|
||
#[derive(Default)] | ||
pub struct Validator { | ||
pub config: ValidateConfig, | ||
} | ||
|
||
#[derive(Deserialize, Default, Debug, Clone)] | ||
pub struct ValidateConfig { | ||
pub ignore_methods: Vec<String>, | ||
} | ||
|
||
#[async_trait] | ||
impl Extension for Validator { | ||
type Config = ValidateConfig; | ||
|
||
async fn from_config(config: &Self::Config, _registry: &ExtensionRegistry) -> Result<Self, anyhow::Error> { | ||
Ok(Self::new(config.clone())) | ||
} | ||
} | ||
|
||
impl Validator { | ||
pub fn new(config: ValidateConfig) -> Self { | ||
Self { config } | ||
} | ||
|
||
pub fn ignore(&self, method: &String) -> bool { | ||
self.config.ignore_methods.contains(method) | ||
} | ||
|
||
pub fn validate(&self, client: Arc<Client>, request: CallRequest, response: CallResult) { | ||
tokio::spawn(async move { | ||
let healthy_endpoints = client.endpoints().iter().filter(|x| x.health().score() > 0); | ||
futures::future::join_all(healthy_endpoints.map(|endpoint| async { | ||
let expected = endpoint | ||
.request( | ||
&request.method, | ||
request.params.clone(), | ||
std::time::Duration::from_secs(30), | ||
) | ||
.await | ||
.map_err(errors::map_error); | ||
|
||
if response != expected { | ||
let request = serde_json::to_string_pretty(&request).unwrap_or_default(); | ||
let actual = match &response { | ||
Ok(value) => serde_json::to_string_pretty(&value).unwrap_or_default(), | ||
Err(e) => e.to_string() | ||
}; | ||
let expected = match &expected { | ||
Ok(value) => serde_json::to_string_pretty(&value).unwrap_or_default(), | ||
Err(e) => e.to_string() | ||
}; | ||
let endpoint_url = endpoint.url(); | ||
tracing::error!("Response mismatch for request:\n{request}\nSubway response:\n{actual}\nEndpoint {endpoint_url} response:\n{expected}"); | ||
} | ||
})).await; | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use async_trait::async_trait; | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
extensions::{client::Client, validator::Validator}, | ||
middlewares::{CallRequest, CallResult, Middleware, MiddlewareBuilder, NextFn, RpcMethod}, | ||
utils::{TypeRegistry, TypeRegistryRef}, | ||
}; | ||
|
||
pub struct ValidateMiddleware { | ||
validator: Arc<Validator>, | ||
client: Arc<Client>, | ||
} | ||
|
||
impl ValidateMiddleware { | ||
pub fn new(validator: Arc<Validator>, client: Arc<Client>) -> Self { | ||
Self { validator, client } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for ValidateMiddleware { | ||
async fn build( | ||
_method: &RpcMethod, | ||
extensions: &TypeRegistryRef, | ||
) -> Option<Box<dyn Middleware<CallRequest, CallResult>>> { | ||
let validate = extensions.read().await.get::<Validator>().unwrap_or_default(); | ||
|
||
let client = extensions | ||
.read() | ||
.await | ||
.get::<Client>() | ||
.expect("Client extension not found"); | ||
Some(Box::new(ValidateMiddleware::new(validate, client))) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Middleware<CallRequest, CallResult> for ValidateMiddleware { | ||
async fn call( | ||
&self, | ||
request: CallRequest, | ||
context: TypeRegistry, | ||
next: NextFn<CallRequest, CallResult>, | ||
) -> CallResult { | ||
let result = next(request.clone(), context).await; | ||
if !self.validator.ignore(&request.method) { | ||
self.validator.validate(self.client.clone(), request, result.clone()); | ||
} | ||
result | ||
} | ||
} |
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