about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlexander_Kabui <alexanderkabua@gmail.com>2026-10-04 13:07:44 +0300
committerAlexander_Kabui <alexanderkabua@gmail.com>2026-10-05 21:54:17 +0300
commit5071434cd3b6c652d95ac8b2026dd173a7fbbace (patch)
tree03e2b33aa0ebe468a89023eaf060073c8e9382b2
parent98b71eb8c080c21ca458961805019170c794fdc7 (diff)
downloadgenenetwork4-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.
-rw-r--r--src/config.rs36
-rw-r--r--src/routes/search.rs29
-rw-r--r--test/python/test_search.py14
-rw-r--r--tests/search.rs14
4 files changed, 82 insertions, 11 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()))?;
 
diff --git a/test/python/test_search.py b/test/python/test_search.py
index d8c5cc5..027501f 100644
--- a/test/python/test_search.py
+++ b/test/python/test_search.py
@@ -23,9 +23,12 @@ class Gn3StubHandler(http.server.BaseHTTPRequestHandler):
 
     def do_GET(self):
         query = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query)
-        if "&&" in query.get("query", [""])[0]:
-            # Mirrors production gn3's shape (live-checked Oct 4):
-            # diagnostic under "error", Python repr under "error_type".
+        if "boom" in query.get("query", [""])[0]:
+            body = b'{"error": "gn3 exploded"}'
+            self.send_response(http.client.INTERNAL_SERVER_ERROR)
+        elif "&&" in query.get("query", [""])[0]:
+            # Mirrors production gn3's shape: diagnostic under
+            # "error", Python repr under "error_type".
             body = json.dumps(
                 {
                     "error": "Syntax: <expression> AND <expression>",
@@ -99,6 +102,11 @@ class SearchTest(ServerTestCase):
         status, _ = self.get("/search?q=alcohol&limit=9999")
         self.assertEqual(status, http.client.OK)
 
+    def test_search_maps_gn3_failures_to_502(self):
+        status, body = self.get("/search?q=boom")
+        self.assertEqual(status, http.client.BAD_GATEWAY)
+        self.assertEqual(body["code"], "backend_error")
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/tests/search.rs b/tests/search.rs
index 5197ffd..c131ed7 100644
--- a/tests/search.rs
+++ b/tests/search.rs
@@ -25,9 +25,9 @@ const FIXTURE: &str = include_str!("../test/data/search_alcohol_phenotype.json")
 /// Stands in for gn3's /api/search during tests.
 async fn serve_gn3(Query(params): Query<HashMap<String, String>>) -> (StatusCode, String) {
     if params.get("query").is_some_and(|q| q.contains("&&")) {
-        // Mirrors production gn3's shape for rejected xapian queries
-        // (live-checked Oct 4): the diagnostic is under "error" and
-        // "error_type" leaks a Python repr.
+        // Mirrors production gn3's shape for rejected xapian
+        // queries: the diagnostic is under "error", and "error_type"
+        // leaks a Python repr.
         return (
             StatusCode::BAD_REQUEST,
             json!({
@@ -37,6 +37,9 @@ async fn serve_gn3(Query(params): Query<HashMap<String, String>>) -> (StatusCode
             .to_string(),
         );
     }
+    if params.get("query").is_some_and(|q| q.contains("boom")) {
+        return (StatusCode::INTERNAL_SERVER_ERROR, String::new());
+    }
     (StatusCode::OK, FIXTURE.to_string())
 }
 
@@ -108,4 +111,9 @@ async fn search_proxy_scenarios() {
     //    bounded request and the envelope comes back fine.
     let (status, _body) = get_json("/search?q=alcohol&limit=9999").await;
     assert_eq!(status, StatusCode::OK);
+
+    // 5. gn3 failures (5xx) surface as one 502 shape.
+    let (status, body) = get_json("/search?q=boom").await;
+    assert_eq!(status, StatusCode::BAD_GATEWAY);
+    assert_eq!(body["code"], "backend_error");
 }