feat: very unstalbe implementation of initial file cache
This commit is contained in:
parent
719e845d5e
commit
c20e80b64c
22 changed files with 340 additions and 32 deletions
|
|
@ -1,5 +1,7 @@
|
|||
use crate::error::RpxyError;
|
||||
use super::cache::RpxyCache;
|
||||
use crate::{error::RpxyError, globals::Globals, log::*, CryptoSource};
|
||||
use async_trait::async_trait;
|
||||
use bytes::Buf;
|
||||
use derive_builder::Builder;
|
||||
use hyper::{
|
||||
body::{Body, HttpBody},
|
||||
|
|
@ -9,6 +11,18 @@ use hyper::{
|
|||
};
|
||||
use hyper_rustls::HttpsConnector;
|
||||
|
||||
fn build_synth_req_for_cache<T>(req: &Request<T>) -> Request<()> {
|
||||
let mut builder = Request::builder()
|
||||
.method(req.method())
|
||||
.uri(req.uri())
|
||||
.version(req.version());
|
||||
// TODO: omit extensions. is this approach correct?
|
||||
for (header_key, header_value) in req.headers() {
|
||||
builder = builder.header(header_key, header_value);
|
||||
}
|
||||
builder.body(()).unwrap()
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
/// Definition of the forwarder that simply forward requests from downstream client to upstream app servers.
|
||||
pub trait ForwardRequest<B> {
|
||||
|
|
@ -17,12 +31,12 @@ pub trait ForwardRequest<B> {
|
|||
}
|
||||
|
||||
#[derive(Builder, Clone)]
|
||||
/// Forwarder struct
|
||||
/// Forwarder struct responsible to cache handling
|
||||
pub struct Forwarder<C, B = Body>
|
||||
where
|
||||
C: Connect + Clone + Sync + Send + 'static,
|
||||
{
|
||||
// TODO: maybe this forwarder definition is suitable for cache handling.
|
||||
cache: Option<RpxyCache>,
|
||||
inner: Client<C, B>,
|
||||
inner_h2: Client<C, B>, // `h2c` or http/2-only client is defined separately
|
||||
}
|
||||
|
|
@ -37,18 +51,63 @@ where
|
|||
{
|
||||
type Error = RpxyError;
|
||||
async fn request(&self, req: Request<B>) -> Result<Response<Body>, Self::Error> {
|
||||
let mut synth_req = None;
|
||||
if self.cache.is_some() {
|
||||
debug!("Search cache first");
|
||||
if let Some(cached_response) = self.cache.as_ref().unwrap().get(&req).await {
|
||||
// if found, return it as response.
|
||||
debug!("Cache hit - Return from cache");
|
||||
return Ok(cached_response);
|
||||
};
|
||||
|
||||
// Synthetic request copy used just for caching (cannot clone request object...)
|
||||
synth_req = Some(build_synth_req_for_cache(&req));
|
||||
}
|
||||
|
||||
// TODO: This 'match' condition is always evaluated at every 'request' invocation. So, it is inefficient.
|
||||
// Needs to be reconsidered. Currently, this is a kind of work around.
|
||||
// This possibly relates to https://github.com/hyperium/hyper/issues/2417.
|
||||
match req.version() {
|
||||
let res = match req.version() {
|
||||
Version::HTTP_2 => self.inner_h2.request(req).await.map_err(RpxyError::Hyper), // handles `h2c` requests
|
||||
_ => self.inner.request(req).await.map_err(RpxyError::Hyper),
|
||||
};
|
||||
|
||||
if self.cache.is_none() {
|
||||
return res;
|
||||
}
|
||||
|
||||
// check cacheability and store it if cacheable
|
||||
let Ok(Some(cache_policy)) = self
|
||||
.cache
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.is_cacheable(synth_req.as_ref(), res.as_ref().ok()) else {
|
||||
return res;
|
||||
};
|
||||
let (parts, body) = res.unwrap().into_parts();
|
||||
// TODO: Inefficient?
|
||||
let Ok(mut bytes) = hyper::body::aggregate(body).await else {
|
||||
return Err(RpxyError::Cache("Failed to write byte buffer"));
|
||||
};
|
||||
let aggregated = bytes.copy_to_bytes(bytes.remaining());
|
||||
|
||||
if let Err(cache_err) = self
|
||||
.cache
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.put(synth_req.unwrap().uri(), &aggregated, cache_policy)
|
||||
.await
|
||||
{
|
||||
error!("{:?}", cache_err);
|
||||
};
|
||||
|
||||
// res
|
||||
Ok(Response::from_parts(parts, Body::from(aggregated)))
|
||||
}
|
||||
}
|
||||
|
||||
impl Forwarder<HttpsConnector<HttpConnector>, Body> {
|
||||
pub async fn new() -> Self {
|
||||
pub async fn new<T: CryptoSource>(globals: &std::sync::Arc<Globals<T>>) -> Self {
|
||||
// let connector = TrustDnsResolver::default().into_rustls_webpki_https_connector();
|
||||
let connector = hyper_rustls::HttpsConnectorBuilder::new()
|
||||
.with_webpki_roots()
|
||||
|
|
@ -64,6 +123,8 @@ impl Forwarder<HttpsConnector<HttpConnector>, Body> {
|
|||
|
||||
let inner = Client::builder().build::<_, Body>(connector);
|
||||
let inner_h2 = Client::builder().http2_only(true).build::<_, Body>(connector_h2);
|
||||
Self { inner, inner_h2 }
|
||||
|
||||
let cache = RpxyCache::new(globals).await;
|
||||
Self { inner, inner_h2, cache }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue