diff options
| author | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-04 13:07:44 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-05 21:54:17 +0300 |
| commit | 5071434cd3b6c652d95ac8b2026dd173a7fbbace (patch) | |
| tree | 03e2b33aa0ebe468a89023eaf060073c8e9382b2 /src | |
| parent | 98b71eb8c080c21ca458961805019170c794fdc7 (diff) | |
| download | genenetwork4-5071434cd3b6c652d95ac8b2026dd173a7fbbace.tar.gz | |
* src/routes/search.rs (search): share one reqwest client with a
total timeout for gn3 calls reqwest::get built a fresh client per query (no keepalive) and set no timeout, while gn3 sets none of its own -- a stalled upstream would hang the handler past any front-proxy timeout. One shared client in a OnceLock now, mirroring the shared-DB-pool pattern. * src/config.rs (Config): add GN3_TIMEOUT_SECS (default 30); normalize GN3_URL to end with a slash. * tests/search.rs test/python/test_search.py (search): one scenario mapping a gn3 5xx to the 502 backend_error shape.
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.rs | 36 | ||||
| -rw-r--r-- | src/routes/search.rs | 29 |
2 files changed, 60 insertions, 5 deletions
diff --git a/src/config.rs b/src/config.rs index a6fc464..4197982 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,6 +20,10 @@ pub struct Config { /// Base URL of the GeneNetwork3 API, used by proxy endpoints. /// Must end with a slash, e.g. http://127.0.0.1:5003/api/. pub gn3_url: String, + /// Seconds before an upstream gn3 call is aborted. gn3 sets no + /// timeout of its own (flask app.run()), so this is the only + /// guard against a stalled upstream. + pub gn3_timeout_secs: u64, /// TCP port to bind. pub port: u16, /// Base URL of the gn-auth server, e.g. "http://127.0.0.1:8081/". @@ -99,6 +103,7 @@ impl Config { "GN3_URL".to_string(), "http://127.0.0.1:5003/api/".to_string(), ); + values.insert("GN3_TIMEOUT_SECS".to_string(), "30".to_string()); // Layers 2 and 3: config files, in increasing priority. for env_key in ["GN4_CONF", "GN4_SECRETS"] { @@ -121,15 +126,21 @@ impl Config { "GN_AUTH_DEV_USERNAME", "GN_AUTH_DEV_PASSWORD", "GN3_URL", + "GN3_TIMEOUT_SECS", ] { if let Ok(value) = std::env::var(key) { values.insert(key.to_string(), value); } } + let gn3_url = normalize_base_url(values["GN3_URL"].clone()); + Config { sql_uri: values["SQL_URI"].clone(), - gn3_url: values["GN3_URL"].clone(), + gn3_url, + gn3_timeout_secs: values["GN3_TIMEOUT_SECS"] + .parse() + .expect("GN3_TIMEOUT_SECS must be a number"), port: values["GENENETWORK4_PORT"] .parse() .expect("GENENETWORK4_PORT must be a number"), @@ -144,6 +155,15 @@ impl Config { } } +/// The proxy appends "search/" to this base; without a trailing slash +/// the result would be ".../apisearch/". Operators may set either form. +fn normalize_base_url(mut url: String) -> String { + if !url.ends_with('/') { + url.push('/'); + } + url +} + /// Parse a `KEY=VALUE` file into a map. Missing/unreadable files are /// ignored so a stale GN4_CONF path never stops the server from /// starting on defaults. @@ -185,6 +205,8 @@ mod tests { "GN_AUTH_DEV_CLIENT_SECRET", "GN_AUTH_DEV_USERNAME", "GN_AUTH_DEV_PASSWORD", + "GN3_URL", + "GN3_TIMEOUT_SECS", ] { if std::env::var(key).is_ok() { eprintln!("skipping: {key} is set in the environment"); @@ -260,4 +282,16 @@ mod tests { fn load_file_missing_file_yields_empty_map() { assert!(load_file("/nonexistent/gn4.conf").is_empty()); } + + #[test] + fn normalize_base_url_appends_missing_slash() { + assert_eq!( + normalize_base_url("http://h:5003/api".into()), + "http://h:5003/api/" + ); + assert_eq!( + normalize_base_url("http://h:5003/api/".into()), + "http://h:5003/api/" + ); + } } diff --git a/src/routes/search.rs b/src/routes/search.rs index 0b736b4..c8f2e1b 100644 --- a/src/routes/search.rs +++ b/src/routes/search.rs @@ -9,12 +9,31 @@ use axum::Json; use axum::extract::{Extension, Query}; use axum::http::StatusCode; use serde::Deserialize; +use std::sync::OnceLock; +use std::time::Duration; use tower_http::request_id::RequestId; use crate::error::ApiError; use crate::request_id_string; use crate::response::Envelope; +/// Shared HTTP client for gn3 upstream calls: one connection pool +/// (keepalive across queries) instead of reqwest::get's fresh client +/// per request, and a total timeout -- gn3 sets none of its own, so +/// without this a stalled upstream hangs the handler past any +/// front-proxy timeout. The timeout is the GN3_TIMEOUT_SECS config key. +static GN3_CLIENT: OnceLock<reqwest::Client> = OnceLock::new(); + +fn gn3_client() -> &'static reqwest::Client { + GN3_CLIENT.get_or_init(|| { + let timeout = Duration::from_secs(crate::config::Config::load().gn3_timeout_secs); + reqwest::Client::builder() + .timeout(timeout) + .build() + .expect("gn3 client builds") + }) +} + /// Query parameters for `GET /search`. /// /// Named for LLM callers (`q`, `type`, `limit`) rather than gn3's @@ -58,9 +77,9 @@ pub struct SearchHit { } /// gn3's error body for rejected queries: {"error": "<diagnostic>", -/// "error_type": "..."}. Live-checked against production (Oct 4): the -/// diagnostic lives under "error"; "error_type" leaks Python reprs -/// (e.g. "b'QueryParserError'"), so only "error" is load-bearing. +/// "error_type": "..."}. The diagnostic lives under "error"; +/// "error_type" leaks Python reprs (e.g. "b'QueryParserError'"), so +/// only "error" is load-bearing. #[utoipa::path(get, path = "/search", summary = "Search traits across GN datasets", @@ -136,7 +155,9 @@ pub async fn search( .with_request_id(rid.clone()) })?; - let response = reqwest::get(url) + let response = gn3_client() + .get(url) + .send() .await .map_err(|e| backend_error("backend_unreachable", e.to_string()))?; |
