Skip to content

fastly: Use async reqwest client #7475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bin/background-worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crates_io_env_vars::{var, var_parsed};
use crates_io_index::RepositoryConfig;
use diesel::r2d2;
use diesel::r2d2::ConnectionManager;
use reqwest::blocking::Client;
use reqwest::Client;
use secrecy::ExposeSecret;
use std::sync::Arc;
use std::thread::sleep;
Expand Down
14 changes: 8 additions & 6 deletions src/fastly.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Context};
use http::HeaderValue;
use reqwest::blocking::Client;
use reqwest::Client;
use secrecy::{ExposeSecret, SecretString};

#[derive(Debug)]
Expand Down Expand Up @@ -34,7 +34,7 @@ impl Fastly {
/// More information on Fastly's APIs for cache invalidations can be found here:
/// <https://developer.fastly.com/reference/api/purging/>
#[instrument(skip(self))]
pub fn invalidate(&self, path: &str) -> anyhow::Result<()> {
pub async fn invalidate(&self, path: &str) -> anyhow::Result<()> {
if path.contains('*') {
return Err(anyhow!(
"wildcard invalidations are not supported for Fastly"
Expand All @@ -49,13 +49,13 @@ impl Fastly {

for domain in domains.iter() {
let url = format!("https://api.fastly.com/purge/{}/{}", domain, path);
self.purge_url(&self.client, &url)?;
self.purge_url(&url).await?;
}

Ok(())
}

fn purge_url(&self, client: &Client, url: &str) -> anyhow::Result<()> {
async fn purge_url(&self, url: &str) -> anyhow::Result<()> {
trace!(?url);

let api_token = self.api_token.expose_secret();
Expand All @@ -66,10 +66,12 @@ impl Fastly {
headers.append("Fastly-Key", api_token);

debug!("sending invalidation request to Fastly");
let response = client
let response = self
.client
.post(url)
.headers(headers)
.send()
.await
.context("failed to send invalidation request to Fastly")?;

let status = response.status();
Expand All @@ -81,7 +83,7 @@ impl Fastly {
}
Err(error) => {
let headers = response.headers().clone();
let body = response.text();
let body = response.text().await;
debug!(
?status,
?headers,
Expand Down
2 changes: 1 addition & 1 deletion src/worker/jobs/dump_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl BackgroundJob for DumpDb {
}

if let Some(fastly) = env.fastly() {
if let Err(error) = fastly.invalidate(&self.target_name) {
if let Err(error) = rt.block_on(fastly.invalidate(&self.target_name)) {
warn!("failed to invalidate Fastly cache: {}", error);
}
}
Expand Down