feat: separated feature cache
This commit is contained in:
parent
8ecc83fe78
commit
32b173966c
11 changed files with 76 additions and 36 deletions
|
|
@ -12,10 +12,11 @@ publish = false
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[features]
|
||||
default = ["http3-quinn", "sticky-cookie"]
|
||||
default = ["http3-quinn", "sticky-cookie", "cache"]
|
||||
http3-quinn = ["quinn", "h3", "h3-quinn", "socket2"]
|
||||
http3-s2n = ["h3", "s2n-quic", "s2n-quic-rustls", "s2n-quic-h3"]
|
||||
sticky-cookie = ["base64", "sha2", "chrono"]
|
||||
cache = ["http-cache-semantics", "lru"]
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
|
|
@ -74,8 +75,8 @@ s2n-quic-h3 = { path = "../submodules/s2n-quic/quic/s2n-quic-h3/", optional = tr
|
|||
s2n-quic-rustls = { path = "../submodules/s2n-quic/quic/s2n-quic-rustls/", optional = true }
|
||||
|
||||
# cache
|
||||
http-cache-semantics = { path = "../submodules/rusty-http-cache-semantics/" }
|
||||
lru = { version = "0.11.0" }
|
||||
http-cache-semantics = { path = "../submodules/rusty-http-cache-semantics/", optional = true }
|
||||
lru = { version = "0.11.0", optional = true }
|
||||
|
||||
# cookie handling for sticky cookie
|
||||
chrono = { version = "0.4.26", default-features = false, features = [
|
||||
|
|
|
|||
|
|
@ -31,3 +31,12 @@ pub mod H3 {
|
|||
#[cfg(feature = "sticky-cookie")]
|
||||
/// For load-balancing with sticky cookie
|
||||
pub const STICKY_COOKIE_NAME: &str = "rpxy_srv_id";
|
||||
|
||||
#[cfg(feature = "cache")]
|
||||
// # of entries in cache
|
||||
pub const MAX_CACHE_ENTRY: usize = 1_000;
|
||||
#[cfg(feature = "cache")]
|
||||
// max size for each file in bytes
|
||||
pub const MAX_CACHE_EACH_SIZE: usize = 65_535;
|
||||
|
||||
// TODO: max cache size in total
|
||||
|
|
|
|||
|
|
@ -9,11 +9,11 @@ use crate::{
|
|||
utils::{BytesName, PathNameBytesExp},
|
||||
};
|
||||
use rustc_hash::FxHashMap as HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
Arc,
|
||||
};
|
||||
use std::{net::SocketAddr, path::PathBuf};
|
||||
use tokio::time::Duration;
|
||||
|
||||
/// Global object containing proxy configurations and shared object like counters.
|
||||
|
|
@ -53,10 +53,14 @@ pub struct ProxyConfig {
|
|||
// experimentals
|
||||
pub sni_consistency: bool, // Handler
|
||||
|
||||
#[cfg(feature = "cache")]
|
||||
pub cache_enabled: bool,
|
||||
pub cache_dir: Option<PathBuf>,
|
||||
pub cache_max_entry: Option<usize>,
|
||||
pub cache_max_each_size: Option<usize>,
|
||||
#[cfg(feature = "cache")]
|
||||
pub cache_dir: Option<std::path::PathBuf>,
|
||||
#[cfg(feature = "cache")]
|
||||
pub cache_max_entry: usize,
|
||||
#[cfg(feature = "cache")]
|
||||
pub cache_max_each_size: usize,
|
||||
|
||||
// All need to make packet acceptor
|
||||
#[cfg(any(feature = "http3-quinn", feature = "http3-s2n"))]
|
||||
|
|
@ -93,10 +97,14 @@ impl Default for ProxyConfig {
|
|||
|
||||
sni_consistency: true,
|
||||
|
||||
#[cfg(feature = "cache")]
|
||||
cache_enabled: false,
|
||||
#[cfg(feature = "cache")]
|
||||
cache_dir: None,
|
||||
cache_max_entry: None,
|
||||
cache_max_each_size: None,
|
||||
#[cfg(feature = "cache")]
|
||||
cache_max_entry: MAX_CACHE_ENTRY,
|
||||
#[cfg(feature = "cache")]
|
||||
cache_max_each_size: MAX_CACHE_EACH_SIZE,
|
||||
|
||||
#[cfg(any(feature = "http3-quinn", feature = "http3-s2n"))]
|
||||
http3: false,
|
||||
|
|
|
|||
|
|
@ -121,14 +121,14 @@ impl RpxyCache {
|
|||
let path = globals.proxy_config.cache_dir.as_ref().unwrap();
|
||||
let cache_file_manager = Arc::new(RwLock::new(CacheFileManager::new(path, &globals.runtime_handle).await));
|
||||
let inner = Arc::new(Mutex::new(LruCache::new(
|
||||
std::num::NonZeroUsize::new(globals.proxy_config.cache_max_entry.unwrap()).unwrap(),
|
||||
std::num::NonZeroUsize::new(globals.proxy_config.cache_max_entry).unwrap(),
|
||||
)));
|
||||
|
||||
Some(Self {
|
||||
cache_file_manager,
|
||||
inner,
|
||||
runtime_handle: globals.runtime_handle.clone(),
|
||||
max_each_size: globals.proxy_config.cache_max_each_size.unwrap(),
|
||||
max_each_size: globals.proxy_config.cache_max_each_size,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
#[cfg(feature = "cache")]
|
||||
use super::cache::{get_policy_if_cacheable, RpxyCache};
|
||||
use crate::{error::RpxyError, globals::Globals, log::*, CryptoSource};
|
||||
#[cfg(feature = "cache")]
|
||||
use crate::log::*;
|
||||
use crate::{error::RpxyError, globals::Globals, CryptoSource};
|
||||
use async_trait::async_trait;
|
||||
#[cfg(feature = "cache")]
|
||||
use bytes::Buf;
|
||||
use derive_builder::Builder;
|
||||
use hyper::{
|
||||
body::{Body, HttpBody},
|
||||
client::{connect::Connect, HttpConnector},
|
||||
|
|
@ -11,6 +14,8 @@ use hyper::{
|
|||
};
|
||||
use hyper_rustls::HttpsConnector;
|
||||
|
||||
#[cfg(feature = "cache")]
|
||||
/// Build synthetic request to cache
|
||||
fn build_synth_req_for_cache<T>(req: &Request<T>) -> Request<()> {
|
||||
let mut builder = Request::builder()
|
||||
.method(req.method())
|
||||
|
|
@ -30,12 +35,12 @@ pub trait ForwardRequest<B> {
|
|||
async fn request(&self, req: Request<B>) -> Result<Response<Body>, Self::Error>;
|
||||
}
|
||||
|
||||
#[derive(Builder, Clone)]
|
||||
/// Forwarder struct responsible to cache handling
|
||||
pub struct Forwarder<C, B = Body>
|
||||
where
|
||||
C: Connect + Clone + Sync + Send + 'static,
|
||||
{
|
||||
#[cfg(feature = "cache")]
|
||||
cache: Option<RpxyCache>,
|
||||
inner: Client<C, B>,
|
||||
inner_h2: Client<C, B>, // `h2c` or http/2-only client is defined separately
|
||||
|
|
@ -50,6 +55,8 @@ where
|
|||
C: Connect + Clone + Sync + Send + 'static,
|
||||
{
|
||||
type Error = RpxyError;
|
||||
|
||||
#[cfg(feature = "cache")]
|
||||
async fn request(&self, req: Request<B>) -> Result<Response<Body>, Self::Error> {
|
||||
let mut synth_req = None;
|
||||
if self.cache.is_some() {
|
||||
|
|
@ -99,10 +106,19 @@ where
|
|||
// res
|
||||
Ok(Response::from_parts(parts, Body::from(aggregated)))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cache"))]
|
||||
async fn request(&self, req: Request<B>) -> Result<Response<Body>, Self::Error> {
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Forwarder<HttpsConnector<HttpConnector>, Body> {
|
||||
pub async fn new<T: CryptoSource>(globals: &std::sync::Arc<Globals<T>>) -> Self {
|
||||
/// Build forwarder
|
||||
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()
|
||||
|
|
@ -119,7 +135,12 @@ impl Forwarder<HttpsConnector<HttpConnector>, Body> {
|
|||
let inner = Client::builder().build::<_, Body>(connector);
|
||||
let inner_h2 = Client::builder().http2_only(true).build::<_, Body>(connector_h2);
|
||||
|
||||
let cache = RpxyCache::new(globals).await;
|
||||
Self { inner, inner_h2, cache }
|
||||
#[cfg(feature = "cache")]
|
||||
{
|
||||
let cache = RpxyCache::new(_globals).await;
|
||||
Self { inner, inner_h2, cache }
|
||||
}
|
||||
#[cfg(not(feature = "cache"))]
|
||||
Self { inner, inner_h2 }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#[cfg(feature = "cache")]
|
||||
mod cache;
|
||||
mod forwarder;
|
||||
mod handler_main;
|
||||
|
|
|
|||
|
|
@ -60,11 +60,14 @@ where
|
|||
if !proxy_config.sni_consistency {
|
||||
info!("Ignore consistency between TLS SNI and Host header (or Request line). Note it violates RFC.");
|
||||
}
|
||||
#[cfg(feature = "cache")]
|
||||
if proxy_config.cache_enabled {
|
||||
info!(
|
||||
"Cache is enabled: cache dir = {:?}",
|
||||
proxy_config.cache_dir.as_ref().unwrap()
|
||||
);
|
||||
} else {
|
||||
info!("Cache is disabled")
|
||||
}
|
||||
|
||||
// build global
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue