FTA7700 commited on
Commit
f3ac5bb
·
1 Parent(s): e7590b3

Reuse a single pooled IPv4-only client for Cloudflare purge calls instead of rebuilding one per request

Browse files
Files changed (1) hide show
  1. api/src/services/stremio_account.rs +19 -7
api/src/services/stremio_account.rs CHANGED
@@ -2,6 +2,7 @@
2
  use crate::error::AppError;
3
  use std::net::SocketAddr;
4
  use std::sync::Arc;
 
5
 
6
  #[derive(Debug, Clone, Deserialize)]
7
  pub struct AddonManifest {
@@ -253,12 +254,23 @@ impl reqwest::dns::Resolve for Ipv4OnlyResolver {
253
  /// with the same class of IPv6-egress issue already documented for Supabase's
254
  /// direct-connection host). Forcing IPv4 here avoids touching the shared
255
  /// `state.http` client used for every other outbound call.
256
- fn build_ipv4_only_client() -> reqwest::Client {
257
- reqwest::Client::builder()
258
- .dns_resolver(Arc::new(Ipv4OnlyResolver::new()))
259
- .timeout(std::time::Duration::from_secs(15))
260
- .build()
261
- .expect("failed to build ipv4-only client")
 
 
 
 
 
 
 
 
 
 
 
262
  }
263
 
264
  pub async fn purge_cloudflare_tags(_client: &reqwest::Client, prefixes: &[String]) {
@@ -281,7 +293,7 @@ pub async fn purge_cloudflare_tags(_client: &reqwest::Client, prefixes: &[String
281
  }
282
 
283
  let body = serde_json::json!({ "tags": tags });
284
- let cf_client = build_ipv4_only_client();
285
  let result = cf_client
286
  .post(CLOUDFLARE_PURGE_URL)
287
  .bearer_auth(&secret)
 
2
  use crate::error::AppError;
3
  use std::net::SocketAddr;
4
  use std::sync::Arc;
5
+ use std::sync::OnceLock;
6
 
7
  #[derive(Debug, Clone, Deserialize)]
8
  pub struct AddonManifest {
 
254
  /// with the same class of IPv6-egress issue already documented for Supabase's
255
  /// direct-connection host). Forcing IPv4 here avoids touching the shared
256
  /// `state.http` client used for every other outbound call.
257
+ static IPV4_ONLY_CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
258
+
259
+ /// Returns a process-wide, lazily-built IPv4-only client, reused across every
260
+ /// call rather than rebuilt from scratch each time. Rebuilding per-call meant
261
+ /// every purge paid a full fresh TCP+TLS handshake cost with no connection
262
+ /// reuse — plausible cause of intermittent post-DNS timeouts observed even
263
+ /// though DNS resolution itself was consistently fast (12-57ms). Reusing one
264
+ /// client lets subsequent calls benefit from a warm, pooled connection.
265
+ fn get_ipv4_only_client() -> &'static reqwest::Client {
266
+ IPV4_ONLY_CLIENT.get_or_init(|| {
267
+ reqwest::Client::builder()
268
+ .dns_resolver(Arc::new(Ipv4OnlyResolver::new()))
269
+ .timeout(std::time::Duration::from_secs(15))
270
+ .pool_idle_timeout(std::time::Duration::from_secs(90))
271
+ .build()
272
+ .expect("failed to build ipv4-only client")
273
+ })
274
  }
275
 
276
  pub async fn purge_cloudflare_tags(_client: &reqwest::Client, prefixes: &[String]) {
 
293
  }
294
 
295
  let body = serde_json::json!({ "tags": tags });
296
+ let cf_client = get_ipv4_only_client();
297
  let result = cf_client
298
  .post(CLOUDFLARE_PURGE_URL)
299
  .bearer_auth(&secret)