Skip to content

Commit 5077c75

Browse files
committed
perf: reuse HTTP client in bridge proxy to reduce CPU overhead
- Add shared reqwest::Client to AppState with connection pooling - Configure pool_max_idle_per_host(50) and pool_idle_timeout(90s) - Reuse client in bridge.rs instead of creating new one per request - Reduces CPU from TLS handshakes and client initialization
1 parent 52d29f8 commit 5077c75

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

crates/platform-server/src/api/bridge.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,8 @@ async fn proxy_to_challenge(
110110
}
111111
};
112112

113-
let client = reqwest::Client::builder()
114-
.timeout(std::time::Duration::from_secs(120))
115-
.build()
116-
.unwrap();
117-
118-
let mut req_builder = client.request(method, &url);
113+
// Use shared HTTP client from AppState (avoids creating new client per request)
114+
let mut req_builder = state.http_client.request(method, &url);
119115
for (key, value) in headers.iter() {
120116
if key != "host" && key != "content-length" {
121117
req_builder = req_builder.header(key, value);

crates/platform-server/src/state.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use platform_bittensor::Metagraph;
1010
use std::collections::HashMap;
1111
use std::collections::HashSet;
1212
use std::sync::Arc;
13-
use std::time::Instant;
13+
use std::time::{Duration, Instant};
1414

1515
/// System metrics from a validator
1616
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
@@ -79,9 +79,21 @@ pub struct AppState {
7979
pub tempo: RwLock<u64>,
8080
/// Current block number from Bittensor
8181
pub current_block: RwLock<u64>,
82+
/// Shared HTTP client for bridge proxy requests (reused to reduce CPU/memory)
83+
pub http_client: reqwest::Client,
8284
}
8385

8486
impl AppState {
87+
/// Create a shared HTTP client for bridge proxy requests
88+
fn create_http_client() -> reqwest::Client {
89+
reqwest::Client::builder()
90+
.timeout(Duration::from_secs(120))
91+
.pool_max_idle_per_host(50) // Keep connections warm
92+
.pool_idle_timeout(Duration::from_secs(90))
93+
.build()
94+
.expect("Failed to create HTTP client")
95+
}
96+
8597
/// Constructor for dynamic orchestration mode
8698
#[allow(dead_code)] // Used by bins/platform
8799
pub fn new_dynamic(
@@ -101,6 +113,7 @@ impl AppState {
101113
metrics_cache: MetricsCache::new(),
102114
tempo: RwLock::new(DEFAULT_TEMPO),
103115
current_block: RwLock::new(0),
116+
http_client: Self::create_http_client(),
104117
}
105118
}
106119

@@ -123,6 +136,7 @@ impl AppState {
123136
metrics_cache: MetricsCache::new(),
124137
tempo: RwLock::new(DEFAULT_TEMPO),
125138
current_block: RwLock::new(0),
139+
http_client: Self::create_http_client(),
126140
}
127141
}
128142

0 commit comments

Comments
 (0)