Restructured cache. Added Redis|Valkey|Dragonfly support.

This commit is contained in:
Ara Sadoyan
2026-10-19 19:13:24 +02:00
parent a2b14b0dea
commit f895ba4554
13 changed files with 426 additions and 45 deletions
+2
View File
@@ -1,3 +1,4 @@
use crate::cache::types::init_cache;
use crate::core::proxyhttp::LB;
use crate::ingress::{APIUpstreamProvider, ConsulProvider, Discovery, FromFileProvider, KubernetesProvider};
use crate::logging::core::{init_access_log, init_access_logging, log_builder};
@@ -84,6 +85,7 @@ impl BackgroundService for LB {
let acme_provider = self.config.acme_dns_provider.clone();
tokio::spawn(async move { refresh_order(certdir, confdir, acme_provider).await });
init_cache(self.config.clone()).await;
init_access_logging(self.config.access_log.clone()).await;
loop {
tokio::select! {
+9 -10
View File
@@ -1,7 +1,8 @@
use crate::auth::authenticate;
use crate::cache::types::GLOBAL_STORAGE;
use crate::core::gethosts::{GetHost, GetHostsReturHeaders};
use crate::logging::core::access_log;
use crate::utils::lazylock::{CACHE_LOCK, CACHE_TTL, EVICTION, LOCALHOST, MEM_CACHE, RATE_LIMITER, REQUESTS_4XX, REVERSE_STORE};
use crate::utils::lazylock::{CACHE_LOCK, CACHE_TTL, EVICTION, LOCALHOST, RATE_LIMITER, REQUESTS_4XX, REVERSE_STORE};
use crate::utils::metrics::*;
use crate::utils::types::{AppConfig, Extraparams, Headers, InnerMap, UpstreamsDashMap, UpstreamsIdMap};
use arc_swap::ArcSwap;
@@ -67,8 +68,7 @@ impl ProxyHttp for LB {
fn cache_key_callback(&self, session: &Session, ctx: &mut Self::CTX) -> Result<CacheKey> {
let host = ctx.hostname.as_deref().unwrap_or("default");
let path = session.req_header().uri.path();
let path = session.req_header().uri.path_and_query().map_or("/", |pq| pq.as_str());
let primary_key = KEY_BUF.with(|buf| {
let mut b = buf.borrow_mut();
b.clear();
@@ -76,15 +76,14 @@ impl ProxyHttp for LB {
b.push_str(path);
b.clone()
});
let secondary_key = session.req_header().uri.path_and_query().map(|pq| pq.as_str()).unwrap_or("/");
Ok(CacheKey::new(primary_key, secondary_key))
Ok(CacheKey::new(primary_key, host /* host is just for debugging */))
}
fn request_cache_filter(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result<()> {
if self.cache_enabled && session.req_header().method == Method::GET {
if let Some(eviction) = EVICTION.get() {
session.cache.enable(&*MEM_CACHE, Some(eviction), None, Some(&*CACHE_LOCK), None);
let has_auth = _ctx.upstream_peer.as_ref().map(|m| m.authorization.is_some()).unwrap_or(false) || _ctx.extraparams.authentication.is_some();
if self.cache_enabled && session.req_header().method == Method::GET && !has_auth {
if let Some(storage) = GLOBAL_STORAGE.get() {
let eviction_mgr = EVICTION.get().map(|e| e as &(dyn pingora_cache::eviction::EvictionManager + Sync));
session.cache.enable(storage.as_ref(), eviction_mgr, None, Some(&*CACHE_LOCK), None);
}
}
Ok(())
+19 -15
View File
@@ -29,20 +29,24 @@ use std::time::Duration;
use std::{fs, thread};
pub fn run() {
default_provider().install_default().expect("Failed to install RusTLS crypto provider");
default_provider().install_default().die("Failed to install RusTLS crypto provider:");
let parameters = Opt::parse_args();
let file = parameters.conf.clone().unwrap();
let file = parameters.conf.clone().die("Failed to clone the config:");
let maincfg = crate::utils::parceyaml::parce_main_config(file.as_str());
let master_server = maincfg.cluster_member.as_deref() == Some("master");
let cache_enabled = maincfg.cache_size_mb.is_some();
CACHE_TTL.set(maincfg.cache_ttl.unwrap_or(60)).unwrap();
if let Some(size) = maincfg.cache_size_mb {
let cache_size = size * 1024 * 1024;
drop(EVICTION.set(Manager::new(cache_size)));
}
let mut server = Server::new(parameters).unwrap();
let cache_enabled = maincfg.cache_enabled.unwrap_or(false);
// ========================================================================== //
CACHE_TTL.set(maincfg.cache_ttl.unwrap_or(60)).die("Failed to set cache TTL");
if cache_enabled {
let cache_size = maincfg.cache_size_mb.unwrap_or(50);
let _ = EVICTION.set(Manager::new(cache_size * 1024 * 1024));
}
// ========================================================================== //
let mut server = Server::new(parameters).die("Failed to start the server:");
server.bootstrap();
let uf_config = Arc::new(DashMap::new());
@@ -106,22 +110,22 @@ pub fn run() {
if let Some(bind_address_tls) = bind_address_tls {
check_priv(bind_address_tls.as_str());
let (tx, rx): (Sender<Vec<CertificateConfig>>, Receiver<Vec<CertificateConfig>>) = channel();
let certs_path = cfg.proxy_configs.clone().unwrap() + "/certificates";
let certs_path = cfg.proxy_configs.clone().die("Failed certificates path") + "/certificates";
if fs::metadata(certs_path.clone()).is_err() {
fs::create_dir_all(certs_path.clone()).unwrap();
fs::create_dir_all(certs_path.clone()).die("Failed to create directories");
}
thread::spawn(move || {
folder_watch(certs_path, tx).unwrap();
folder_watch(certs_path, tx).die("Failed to spawn certificates channel");
});
let certificate_configs = rx.recv().unwrap();
let certificate_configs = rx.recv().die("Failed to spawn certificates channel");
let first_set = load::Certificates::new(&certificate_configs, grade.as_str()).unwrap_or_else(|| panic!("Unable to load initial certificate info"));
let certificates = Arc::new(ArcSwap::from_pointee(first_set));
let certs_for_callback = certificates.clone();
let certs_for_watcher = certificates.clone();
let new_certs = load::Certificates::new(&certificate_configs, grade.as_str());
certs_for_watcher.store(Arc::new(new_certs.unwrap()));
certs_for_watcher.store(Arc::new(new_certs.die("Failed to build the watcher")));
let mut tls_settings =
TlsSettings::intermediate(&certs_for_callback.load().default_cert_path, &certs_for_callback.load().default_key_path).expect("unable to load or parse cert/key");
@@ -162,7 +166,7 @@ pub fn run() {
if let Err(e) = write_pid_file(pf.as_str()) {
panic!("Failed to write PID file: {} : {}", pf, e);
}
let mut signals = Signals::new([SIGINT, SIGTERM, SIGQUIT]).unwrap();
let mut signals = Signals::new([SIGINT, SIGTERM, SIGQUIT]).die("Failed to create signals");
if let Some(sig) = signals.forever().next() {
match sig {
SIGINT => info!("SIGINT received! Exiting..."),