From db8874467e0fb064da1d73af3481c25cd9b8ef24 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 9 Aug 2023 00:05:26 +0200 Subject: [PATCH] gatherer: use one shared http_client --- gatherer/Cargo.toml | 2 +- gatherer/src/http_server.rs | 4 +++- gatherer/src/http_server/token_collect.rs | 13 +++++++++---- gatherer/src/http_server/token_donate.rs | 4 ++-- gatherer/src/main.rs | 11 +++++++++++ gatherer/src/oauth.rs | 6 ++---- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/gatherer/Cargo.toml b/gatherer/Cargo.toml index 1d1e3c9..022b623 100644 --- a/gatherer/Cargo.toml +++ b/gatherer/Cargo.toml @@ -21,5 +21,5 @@ askama = "0.11" metrics = "0.20" metrics-util = "0.14" metrics-exporter-prometheus = "0.11" -reqwest = "0.11" +reqwest = { version = "0.11", features = ["json", "trust-dns"] } urlencoding = "1" diff --git a/gatherer/src/http_server.rs b/gatherer/src/http_server.rs index bc655e9..70d88c5 100644 --- a/gatherer/src/http_server.rs +++ b/gatherer/src/http_server.rs @@ -39,6 +39,7 @@ type Languages = Vec; pub struct ServerState { store: Store, db: Database, + http_client: reqwest::Client, } impl ServerState { @@ -209,6 +210,7 @@ pub async fn start( listen_port: u16, store: Store, db: Database, + http_client: reqwest::Client, firehose_factory: FirehoseFactory, recorder: PrometheusHandle, ) { @@ -222,7 +224,7 @@ pub async fn start( .route("/token/donate", get(token_donate::get_token_donate).post(token_donate::post_token_donate)) .route("/token/collect/:host", get(token_collect::get_token_collect)) .route("/token/thanks", get(token_collect::get_token_thanks)) - .layer(Extension(ServerState { store, db })) + .layer(Extension(ServerState { store, db, http_client })) .layer(Extension(firehose_factory)) .route("/metrics", get(|| async move { recorder.render().into_response() diff --git a/gatherer/src/http_server/token_collect.rs b/gatherer/src/http_server/token_collect.rs index d1a73af..98feaf5 100644 --- a/gatherer/src/http_server/token_collect.rs +++ b/gatherer/src/http_server/token_collect.rs @@ -10,7 +10,12 @@ use crate::{ oauth, }; -async fn collect_token(db: Database, host: &str, code: String) -> Result<(), String> { +async fn collect_token( + db: Database, + http_client: &reqwest::Client, + host: &str, + code: String, +) -> Result<(), String> { // try a few registered apps until one works for (client_id, client_secret) in db.get_apps(&host).await .map_err(|e| format!("{}", e))? @@ -19,7 +24,7 @@ async fn collect_token(db: Database, host: &str, code: String) -> Result<(), Str client_id, client_secret, }; - match app.obtain_token(&host, code.clone()).await { + match app.obtain_token(http_client, &host, code.clone()).await { Ok(token) => { db.add_token(&host, &app.client_id, &token).await .expect("db.add_token"); @@ -43,11 +48,11 @@ pub struct OAuthCode { } pub async fn get_token_collect( - Extension(ServerState { db, .. }): Extension, + Extension(ServerState { db, http_client, .. }): Extension, extract::Path(host): extract::Path, extract::Query(OAuthCode { code }): extract::Query, ) -> impl IntoResponse { - match collect_token(db, &host, code.clone()).await { + match collect_token(db, &http_client, &host, code.clone()).await { Ok(()) => ( StatusCode::SEE_OTHER, diff --git a/gatherer/src/http_server/token_donate.rs b/gatherer/src/http_server/token_donate.rs index 8582d4e..a91ceba 100644 --- a/gatherer/src/http_server/token_donate.rs +++ b/gatherer/src/http_server/token_donate.rs @@ -42,7 +42,7 @@ impl IntoResponse for PostTokenDonateResult { } pub async fn post_token_donate( - Extension(ServerState { db, .. }): Extension, + Extension(ServerState { db, http_client, .. }): Extension, extract::Form(form): extract::Form, ) -> PostTokenDonateResult { let apps = db.get_apps(&form.instance).await @@ -57,7 +57,7 @@ pub async fn post_token_donate( }; } else { // register a new app with this instance - app = match oauth::Application::register(&form.instance).await { + app = match oauth::Application::register(&http_client, &form.instance).await { Ok(app) => app, Err(e) => { tracing::error!("Canont register OAuth app: {}", e); diff --git a/gatherer/src/main.rs b/gatherer/src/main.rs index 63dd688..06a385c 100644 --- a/gatherer/src/main.rs +++ b/gatherer/src/main.rs @@ -26,6 +26,16 @@ async fn main() { .install_recorder() .unwrap(); + let http_client = reqwest::Client::builder() + .timeout(Duration::from_secs(10)) + .pool_max_idle_per_host(0) + .user_agent( + format!("{}/{} (+https://fedi.buzz/)", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")) + ) + .trust_dns(true) + .build() + .expect("reqwest::Client"); + cave::systemd::status("Connecting to database"); let db = cave::db::Database::connect(&config.database).await; cave::systemd::status("Starting redis client"); @@ -37,6 +47,7 @@ async fn main() { config.listen_port, store, db, + http_client, firehose_factory, recorder, ); diff --git a/gatherer/src/oauth.rs b/gatherer/src/oauth.rs index 2ba33d3..45144c2 100644 --- a/gatherer/src/oauth.rs +++ b/gatherer/src/oauth.rs @@ -31,7 +31,7 @@ pub struct Application { const SCOPES: &str = "read:statuses"; impl Application { - pub async fn register(host: &str) -> Result { + pub async fn register(client: &reqwest::Client, host: &str) -> Result { let url = format!("https://{}/api/v1/apps", host); let form = AppRegistration { client_name: "#FediBuzz".to_string(), @@ -39,7 +39,6 @@ impl Application { redirect_uris: Self::generate_redirect_url(host), scopes: SCOPES.to_string(), }; - let client = reqwest::Client::new(); let res = client.post(url) .form(&form) .send() @@ -63,7 +62,7 @@ impl Application { ) } - pub async fn obtain_token(&self, host: &str, code: String) -> Result { + pub async fn obtain_token(&self, client: &reqwest::Client, host: &str, code: String) -> Result { let url = format!("https://{}/oauth/token", host); let form = TokenRequest { grant_type: "authorization_code".to_string(), @@ -73,7 +72,6 @@ impl Application { redirect_uri: Self::generate_redirect_url(host), code, }; - let client = reqwest::Client::new(); let res: TokenResult = client.post(url) .form(&form) .send()