From 5bba31ace82c6098baa86df4ae01d53dbdf78e73 Mon Sep 17 00:00:00 2001 From: ttatsato Date: Fri, 24 Apr 2026 22:54:55 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20Fixed=20the=20issue=20where=20a=20?= =?UTF-8?q?=E2=80=9Creqwuest=E2=80=9D=20client=20was=20being=20created=20w?= =?UTF-8?q?ith=20every=20request?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 25 +++++++++++++++++++++++-- src/main.rs | 9 ++++++++- src/routes/proxy.rs | 4 ++-- tests/api_test.rs | 1 + 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index deacfae..6934af4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,9 @@ pub mod routes; pub mod utils; use axum::{ - Router, middleware as axum_middleware, + Router, + extract::FromRef, + middleware as axum_middleware, routing::{get, post}, }; use sqlx::PgPool; @@ -28,11 +30,30 @@ use routes::proxy::proxy; use routes::system::quota_sync::sync_to_db; use std::sync::Arc; +#[derive(Clone)] +pub struct AppState { + pub pool: PgPool, + pub http_client: reqwest::Client, +} + +impl FromRef for PgPool { + fn from_ref(state: &AppState) -> PgPool { + state.pool.clone() + } +} + +impl FromRef for reqwest::Client { + fn from_ref(state: &AppState) -> reqwest::Client { + state.http_client.clone() + } +} + pub fn create_router( pool: PgPool, rate_limiter: Arc, auth_cache: Arc, auth_cache_ttl_secs: u64, + http_client: reqwest::Client, ) -> Router { let protected_routes = Router::new() .route("/proxy/test", get(|| async { "ok" })) @@ -69,5 +90,5 @@ pub fn create_router( public_routes .merge(protected_routes) .merge(system_routes) - .with_state(pool) + .with_state(AppState { pool, http_client }) } diff --git a/src/main.rs b/src/main.rs index bac95d0..56b55bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,14 @@ async fn main() { } let port = std::env::var("API_PORT").unwrap_or_else(|_| "8080".to_string()); - let app = create_router(pool, rate_limiter, valkey_auth_cache, ttl_seconds); + let http_client = reqwest::Client::new(); + let app = create_router( + pool, + rate_limiter, + valkey_auth_cache, + ttl_seconds, + http_client, + ); let listener = tokio::net::TcpListener::bind("0.0.0.0:".to_string() + &port) .await diff --git a/src/routes/proxy.rs b/src/routes/proxy.rs index 9afc9dd..d0d5d9a 100644 --- a/src/routes/proxy.rs +++ b/src/routes/proxy.rs @@ -14,6 +14,7 @@ use sqlx::PgPool; // /proxy/{name}/{*rest_path} のリクエストを upstream_services の base_url に転送する pub async fn proxy( State(pool): State, + State(client): State, Extension(authed): Extension, Path((name, rest_path)): Path<(String, String)>, request: Request, @@ -62,8 +63,7 @@ pub async fn proxy( })? .to_bytes(); - // reqwest で転送 - let client = reqwest::Client::new(); + // reqwest で転送(共有 Client を利用してコネクションプール / keep-alive を有効化) let mut req = client .request(to_reqwest_method(&method), &target_url) .body(body_bytes.to_vec()); diff --git a/tests/api_test.rs b/tests/api_test.rs index b44d16e..ad90b17 100644 --- a/tests/api_test.rs +++ b/tests/api_test.rs @@ -38,6 +38,7 @@ async fn setup() -> (axum::Router, PgPool, Arc) { rate_limiter.clone(), auth_cache, auth_cache_ttl_secs, + reqwest::Client::new(), ); (app, pool, rate_limiter) }