httpd: Add a cache for /tree

This improves response time of:

- GET /api/v1/projects/<RID>/tree/<OID>/ from 205.71ms down to 376.4µs
- GET /api/v1/projects/<RID>/tree/<OID>/.nix from 111.06ms down to 351.70µs

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex
2023-07-25 13:10:58 +02:00
committed by Alexis Sellier
parent 3fcadc5058
commit 2e25250be4
7 changed files with 64 additions and 4 deletions
+1
View File
@@ -24,6 +24,7 @@ fastrand = { version = "1.9.0" }
flate2 = { version = "1" }
hyper = { version = "0.14.17", default-features = false }
lexopt = { version = "0.2.1" }
lru = { version = "0.10.0" }
nonempty = { version = "0.8.1", features = ["serialize"] }
radicle-surf = { version = "0.14.0", default-features = false, features = ["serde"] }
serde = { version = "1", features = ["derive"] }
+6 -1
View File
@@ -25,6 +25,9 @@ mod error;
mod json;
mod v1;
use crate::cache::Cache;
use crate::Options;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Identifier for sessions
@@ -34,13 +37,15 @@ type SessionId = String;
pub struct Context {
profile: Arc<Profile>,
sessions: Arc<RwLock<HashMap<SessionId, auth::Session>>>,
cache: Option<Cache>,
}
impl Context {
pub fn new(profile: Arc<Profile>) -> Self {
pub fn new(profile: Arc<Profile>, options: &Options) -> Self {
Self {
profile,
sessions: Default::default(),
cache: options.cache.map(Cache::new),
}
}
+12
View File
@@ -309,12 +309,24 @@ async fn tree_handler(
State(ctx): State<Context>,
Path((project, sha, path)): Path<(Id, Oid, String)>,
) -> impl IntoResponse {
if let Some(ref cache) = ctx.cache {
let cache = &mut cache.tree.lock().await;
if let Some(response) = cache.get(&(project, sha, path.clone())) {
return Ok::<_, Error>(Json(response.clone()));
}
}
let storage = &ctx.profile.storage;
let repo = Repository::open(paths::repository(storage, &project))?;
let tree = repo.tree(sha, &path)?;
let stats = repo.stats_from(&sha)?;
let response = api::json::tree(&tree, &path, &stats);
if let Some(cache) = ctx.cache {
let cache = &mut cache.tree.lock().await;
cache.put((project, sha, path.clone()), response.clone());
}
Ok::<_, Error>(Json(response))
}
+22
View File
@@ -0,0 +1,22 @@
use std::num::NonZeroUsize;
use std::sync::Arc;
use lru::LruCache;
use tokio::sync::Mutex;
use radicle::prelude::Id;
use radicle_surf::Oid;
#[derive(Clone)]
pub struct Cache {
pub tree: Arc<Mutex<LruCache<(Id, Oid, String), serde_json::Value>>>,
}
impl Cache {
/// Creates a new cache of the given size.
pub fn new(size: NonZeroUsize) -> Self {
Cache {
tree: Arc::new(Mutex::new(LruCache::new(size))),
}
}
}
+8 -1
View File
@@ -4,6 +4,7 @@ pub mod error;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::num::NonZeroUsize;
use std::process::Command;
use std::str;
use std::sync::Arc;
@@ -24,16 +25,21 @@ use tracing_extra::{tracing_middleware, ColoredStatus, Paint, RequestId, Tracing
mod api;
mod axum_extra;
mod cache;
mod git;
mod raw;
#[cfg(test)]
mod test;
mod tracing_extra;
/// Default cache HTTP size.
pub const DEFAULT_CACHE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(100) };
#[derive(Debug, Clone)]
pub struct Options {
pub aliases: HashMap<String, Id>,
pub listen: SocketAddr,
pub cache: Option<NonZeroUsize>,
}
/// Run the Server.
@@ -101,7 +107,7 @@ pub async fn run(options: Options) -> anyhow::Result<()> {
/// Create a router consisting of other sub-routers.
fn router(options: Options, profile: Profile) -> anyhow::Result<Router> {
let profile = Arc::new(profile);
let ctx = api::Context::new(profile.clone());
let ctx = api::Context::new(profile.clone(), &options);
let api_router = api::router(ctx);
let git_router = git::router(profile.clone(), options.aliases);
@@ -132,6 +138,7 @@ mod routes {
super::Options {
aliases: HashMap::new(),
listen: SocketAddr::from(([0, 0, 0, 0], 8080)),
cache: None,
},
test::profile(tmp.path(), [0xff; 32]),
)
+8 -1
View File
@@ -1,3 +1,4 @@
use std::num::NonZeroUsize;
use std::{collections::HashMap, process};
use radicle::prelude::Id;
@@ -51,6 +52,7 @@ fn parse_options() -> Result<httpd::Options, lexopt::Error> {
let mut parser = lexopt::Parser::from_env();
let mut listen = None;
let mut aliases = HashMap::new();
let mut cache = Some(httpd::DEFAULT_CACHE_SIZE);
while let Some(arg) = parser.next()? {
match arg {
@@ -64,8 +66,12 @@ fn parse_options() -> Result<httpd::Options, lexopt::Error> {
aliases.insert(alias, id);
}
Long("cache") => {
let size = parser.value()?.parse()?;
cache = NonZeroUsize::new(size);
}
Long("help") | Short('h') => {
println!("usage: radicle-httpd [--listen <addr>] [--alias <name> <rid>]..");
println!("usage: radicle-httpd [--listen <addr>] [--alias <name> <rid>] [--cache <size>]..");
process::exit(0);
}
_ => return Err(arg.unexpected()),
@@ -74,5 +80,6 @@ fn parse_options() -> Result<httpd::Options, lexopt::Error> {
Ok(httpd::Options {
aliases,
listen: listen.unwrap_or_else(|| ([0, 0, 0, 0], 8080).into()),
cache,
})
}
+7 -1
View File
@@ -203,7 +203,13 @@ fn seed_with_signer<G: Signer>(dir: &Path, profile: radicle::Profile, signer: &G
)
.unwrap();
Context::new(Arc::new(profile))
let options = crate::Options {
aliases: std::collections::HashMap::new(),
listen: std::net::SocketAddr::from(([0, 0, 0, 0], 8080)),
cache: Some(crate::DEFAULT_CACHE_SIZE),
};
Context::new(Arc::new(profile), &options)
}
/// Adds an authorized session to the Context::sessions HashMap.