use smol for client

This commit is contained in:
Pascal Engélibert 2026-03-08 10:56:34 +01:00
commit 9c70eea4f1
5 changed files with 608 additions and 468 deletions

View file

@ -2,7 +2,12 @@ use crate::{cache, config::Config, repo::ReadRepoMetadataError};
use askama::Template;
use log::error;
use std::{collections::HashMap, io::ErrorKind, path::PathBuf, sync::{Arc, Mutex}};
use std::{
collections::HashMap,
io::ErrorKind,
path::PathBuf,
sync::{Arc, Mutex},
};
use trillium::{Conn, Handler};
use trillium_router::{Router, RouterConnExt};
@ -15,7 +20,9 @@ pub fn make_router(config: Arc<Config>) -> impl Handler {
hl_registry.link_grammars();
let hl_registry = Arc::new(hl_registry);
let mut metadata_cache = Arc::new(Mutex::new(cache::Cache::<String, HashMap<String, String>>::default()));
let mut metadata_cache = Arc::new(Mutex::new(
cache::Cache::<String, HashMap<String, String>>::default(),
));
(
trillium_caching_headers::CachingHeaders::new(),
@ -47,26 +54,30 @@ pub fn make_router(config: Arc<Config>) -> impl Handler {
let Some(repo_hash_str) = conn.param("hash") else {
return conn.with_status(401);
};
let cache_fetch = |key| {
let mut repo_hash = [0; 32];
if base64_turbo::URL_SAFE.decode_into(repo_hash_str, &mut repo_hash) != Ok(32) {
if base64_turbo::URL_SAFE.decode_into(repo_hash_str, &mut repo_hash)
!= Ok(32)
{
return None;
}
let repo_dir = PathBuf::from(&config.data_dir).join(crate::SUBDIR_REPOS).join(repo_hash_str);
let repo_dir = PathBuf::from(&config.data_dir)
.join(crate::SUBDIR_REPOS)
.join(repo_hash_str);
let repo_metadata =
match crate::repo::RepoMetadata::read_from_file(&config, &repo_dir) {
Ok(v) => v,
Err(e) => {
if let ReadRepoMetadataError::CannotOpenFile(e) = &e {
if e.kind() == ErrorKind::NotFound {
return None;
match crate::repo::RepoMetadata::read_from_file(&config, &repo_dir) {
Ok(v) => v,
Err(e) => {
if let ReadRepoMetadataError::CannotOpenFile(e) = &e {
if e.kind() == ErrorKind::NotFound {
return None;
}
}
error!("Reading repo metadata: {e:?}");
return None;
}
error!("Reading repo metadata: {e:?}");
return None;
}
};
};
let mut files = HashMap::new();
for file in repo_metadata.iter_files() {
match file {
@ -81,7 +92,10 @@ pub fn make_router(config: Arc<Config>) -> impl Handler {
Some(files)
};
// TODO replace mutex with better thing (less contention or async mutex)
metadata_cache.lock().unwrap().fetch(repo_hash_str.to_string(), cache_fetch);
metadata_cache
.lock()
.unwrap()
.fetch(repo_hash_str.to_string(), cache_fetch);
let hl_options = giallo::HighlightOptions::new(
"py",