Speedup "zola check" command by reusing the Client (#2171)

* Reuse Client when checking urls and add timeout for requests
This commit is contained in:
Marcel 2023-04-06 23:21:01 +02:00 committed by Vincent Prouillet
parent 201d674da1
commit c0dd38b234
1 changed files with 8 additions and 5 deletions

View File

@ -30,6 +30,13 @@ pub fn message(res: &Result) -> String {
// Keep history of link checks so a rebuild doesn't have to check again // Keep history of link checks so a rebuild doesn't have to check again
static LINKS: Lazy<Arc<RwLock<HashMap<String, Result>>>> = static LINKS: Lazy<Arc<RwLock<HashMap<String, Result>>>> =
Lazy::new(|| Arc::new(RwLock::new(HashMap::new()))); Lazy::new(|| Arc::new(RwLock::new(HashMap::new())));
// Make sure to create only a single Client so that we can reuse the connections
static CLIENT: Lazy<Client> = Lazy::new(|| {
Client::builder()
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
.build()
.expect("reqwest client build")
});
pub fn check_url(url: &str, config: &LinkChecker) -> Result { pub fn check_url(url: &str, config: &LinkChecker) -> Result {
{ {
@ -44,15 +51,11 @@ pub fn check_url(url: &str, config: &LinkChecker) -> Result {
headers.append(ACCEPT, "*/*".parse().unwrap()); headers.append(ACCEPT, "*/*".parse().unwrap());
// TODO: pass the client to the check_url, do not pass the config // TODO: pass the client to the check_url, do not pass the config
let client = Client::builder()
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
.build()
.expect("reqwest client build");
let check_anchor = !config.skip_anchor_prefixes.iter().any(|prefix| url.starts_with(prefix)); let check_anchor = !config.skip_anchor_prefixes.iter().any(|prefix| url.starts_with(prefix));
// Need to actually do the link checking // Need to actually do the link checking
let res = match client.get(url).headers(headers).send() { let res = match CLIENT.get(url).headers(headers).send() {
Ok(ref mut response) if check_anchor && has_anchor(url) => { Ok(ref mut response) if check_anchor && has_anchor(url) => {
let body = { let body = {
let mut buf: Vec<u8> = vec![]; let mut buf: Vec<u8> = vec![];