Compare commits

...

24 Commits

Author SHA1 Message Date
5dccbbc9d1 v25.11.23
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-17 12:22:51 +00:00
92d7113c6c fix(rustproxy-http,rustproxy-metrics): reduce per-frame metrics overhead by batching body byte accounting 2026-03-17 12:22:51 +00:00
8f6bb30367 v25.11.22
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-17 12:12:24 +00:00
ef9bac80ff fix(rustproxy-http): reuse healthy HTTP/2 upstream connections after requests with bodies 2026-03-17 12:12:24 +00:00
9c78701038 v25.11.21
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-17 11:33:34 +00:00
26fd9409a7 fix(rustproxy-http): reuse pooled HTTP/2 connections for requests with and without bodies 2026-03-17 11:33:34 +00:00
cfff128499 v25.11.20
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-17 01:32:35 +00:00
3baff354bd fix(rustproxy-http): avoid downgrading cached backend protocol on H2 stream errors 2026-03-17 01:32:35 +00:00
c2eacd1b30 v25.11.19
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 2s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-16 20:53:39 +00:00
1fdbfcf0aa fix(rustproxy-http): avoid reusing pooled HTTP/2 connections for requests with bodies to prevent upload flow-control stalls 2026-03-16 20:53:39 +00:00
9b184acc8c v25.11.18
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-16 17:42:14 +00:00
b475968f4e fix(repo): no changes to commit 2026-03-16 17:42:14 +00:00
878eab6e88 v25.11.17
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-16 14:30:43 +00:00
77abe0804d fix(rustproxy-http): prevent stale HTTP/2 connection drivers from evicting newer pooled connections 2026-03-16 14:30:43 +00:00
ae0342d018 v25.11.16
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-16 13:58:22 +00:00
365981d9cf fix(repo): no changes to commit 2026-03-16 13:58:22 +00:00
2cc0ff0030 v25.11.15
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-16 13:54:56 +00:00
72935e7ee0 fix(rustproxy-http): implement vectored write support for backend streams 2026-03-16 13:54:56 +00:00
61db285e04 v25.11.14
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-16 13:44:56 +00:00
d165829022 fix(rustproxy-http): forward vectored write support in ShutdownOnDrop AsyncWrite wrapper 2026-03-16 13:44:56 +00:00
5e6cf391ab v25.11.13
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-16 13:17:02 +00:00
2b1a21c599 fix(rustproxy-http): remove hot-path debug logging from HTTP/1 connection pool hits 2026-03-16 13:17:02 +00:00
b8e1c9f3cf v25.11.12
Some checks failed
Default (tags) / security (push) Failing after 1s
Default (tags) / test (push) Failing after 1s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2026-03-16 13:12:24 +00:00
c65369540c fix(rustproxy-http): remove connection pool hit logging and keep logging limited to actual failures 2026-03-16 13:12:24 +00:00
8 changed files with 258 additions and 93 deletions

View File

@@ -1,5 +1,75 @@
# Changelog # Changelog
## 2026-03-17 - 25.11.23 - fix(rustproxy-http,rustproxy-metrics)
reduce per-frame metrics overhead by batching body byte accounting
- Buffer HTTP body byte counts and flush them every 64 KB, at end of stream, and on drop to keep totals accurate while preserving throughput sampling.
- Skip zero-value counter updates in metrics collection to avoid unnecessary atomic and DashMap operations for the unused direction.
## 2026-03-17 - 25.11.22 - fix(rustproxy-http)
reuse healthy HTTP/2 upstream connections after requests with bodies
- Registers successful HTTP/2 connections in the pool regardless of whether the proxied request included a body
- Continues to avoid pooling upstream connections that returned 502 Bad Gateway responses
## 2026-03-17 - 25.11.21 - fix(rustproxy-http)
reuse pooled HTTP/2 connections for requests with and without bodies
- remove the bodyless-request restriction from HTTP/2 pool checkout
- always return successful HTTP/2 senders to the connection pool after requests
## 2026-03-17 - 25.11.20 - fix(rustproxy-http)
avoid downgrading cached backend protocol on H2 stream errors
- Treat HTTP/2 stream-level failures as retryable request errors instead of evidence that the backend only supports HTTP/1.1
- Keep protocol cache entries unchanged after successful H2 handshakes so future requests continue using HTTP/2
- Lower log severity for this fallback path from warning to debug while still recording backend H2 failure metrics
## 2026-03-16 - 25.11.19 - fix(rustproxy-http)
avoid reusing pooled HTTP/2 connections for requests with bodies to prevent upload flow-control stalls
- Limit HTTP/2 pool checkout to bodyless requests such as GET, HEAD, and DELETE
- Skip re-registering HTTP/2 connections in the pool after requests that send a body
- Prevent stalled uploads caused by depleted connection-level flow control windows on reused HTTP/2 connections
## 2026-03-16 - 25.11.18 - fix(repo)
no changes to commit
## 2026-03-16 - 25.11.17 - fix(rustproxy-http)
prevent stale HTTP/2 connection drivers from evicting newer pooled connections
- add generation IDs to pooled HTTP/2 senders so pool removal only affects the matching connection
- update HTTP/2 proxy and retry paths to register generation-tagged connections and skip eviction before registration completes
## 2026-03-16 - 25.11.16 - fix(repo)
no changes to commit
## 2026-03-16 - 25.11.15 - fix(rustproxy-http)
implement vectored write support for backend streams
- Add poll_write_vectored forwarding for both plain and TLS backend stream variants
- Expose is_write_vectored so the proxy can correctly report vectored write capability
## 2026-03-16 - 25.11.14 - fix(rustproxy-http)
forward vectored write support in ShutdownOnDrop AsyncWrite wrapper
- Implements poll_write_vectored by delegating to the wrapped writer
- Exposes is_write_vectored so the wrapper preserves underlying AsyncWrite capabilities
## 2026-03-16 - 25.11.13 - fix(rustproxy-http)
remove hot-path debug logging from HTTP/1 connection pool hits
- Stops emitting debug logs when reusing HTTP/1 idle connections in the connection pool.
- Keeps pool hit behavior unchanged while reducing overhead on a frequently executed path.
## 2026-03-16 - 25.11.12 - fix(rustproxy-http)
remove connection pool hit logging and keep logging limited to actual failures
- Removes debug and warning logs for HTTP/2 connection pool hits and age checks.
- Keeps pool behavior unchanged while reducing noisy per-request logging in the Rust HTTP proxy layer.
## 2026-03-16 - 25.11.11 - fix(rustproxy-http) ## 2026-03-16 - 25.11.11 - fix(rustproxy-http)
improve HTTP/2 proxy error logging with warning-level connection failures and debug error details improve HTTP/2 proxy error logging with warning-level connection failures and debug error details

View File

@@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartproxy", "name": "@push.rocks/smartproxy",
"version": "25.11.11", "version": "25.11.23",
"private": false, "private": false,
"description": "A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.", "description": "A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@@ -4,13 +4,13 @@
//! HTTP/2 connections are multiplexed (clone the sender for each request). //! HTTP/2 connections are multiplexed (clone the sender for each request).
use std::sync::Arc; use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use bytes::Bytes; use bytes::Bytes;
use dashmap::DashMap; use dashmap::DashMap;
use http_body_util::combinators::BoxBody; use http_body_util::combinators::BoxBody;
use hyper::client::conn::{http1, http2}; use hyper::client::conn::{http1, http2};
use tracing::{debug, warn};
/// Maximum idle connections per backend key. /// Maximum idle connections per backend key.
const MAX_IDLE_PER_KEY: usize = 16; const MAX_IDLE_PER_KEY: usize = 16;
@@ -38,10 +38,13 @@ struct IdleH1 {
idle_since: Instant, idle_since: Instant,
} }
/// A pooled HTTP/2 sender (multiplexed, Clone-able). /// A pooled HTTP/2 sender (multiplexed, Clone-able) with a generation tag.
struct PooledH2 { struct PooledH2 {
sender: http2::SendRequest<BoxBody<Bytes, hyper::Error>>, sender: http2::SendRequest<BoxBody<Bytes, hyper::Error>>,
created_at: Instant, created_at: Instant,
/// Unique generation ID. Connection drivers use this to only remove their OWN
/// entry, preventing phantom eviction when multiple connections share the same key.
generation: u64,
} }
/// Backend connection pool. /// Backend connection pool.
@@ -50,6 +53,8 @@ pub struct ConnectionPool {
h1_pool: Arc<DashMap<PoolKey, Vec<IdleH1>>>, h1_pool: Arc<DashMap<PoolKey, Vec<IdleH1>>>,
/// HTTP/2 multiplexed connections indexed by backend key. /// HTTP/2 multiplexed connections indexed by backend key.
h2_pool: Arc<DashMap<PoolKey, PooledH2>>, h2_pool: Arc<DashMap<PoolKey, PooledH2>>,
/// Monotonic generation counter for H2 pool entries.
h2_generation: AtomicU64,
/// Handle for the background eviction task. /// Handle for the background eviction task.
eviction_handle: Option<tokio::task::JoinHandle<()>>, eviction_handle: Option<tokio::task::JoinHandle<()>>,
} }
@@ -69,6 +74,7 @@ impl ConnectionPool {
Self { Self {
h1_pool, h1_pool,
h2_pool, h2_pool,
h2_generation: AtomicU64::new(0),
eviction_handle: Some(eviction_handle), eviction_handle: Some(eviction_handle),
} }
} }
@@ -82,7 +88,7 @@ impl ConnectionPool {
while let Some(idle) = idles.pop() { while let Some(idle) = idles.pop() {
// Check if the connection is still alive and ready // Check if the connection is still alive and ready
if idle.idle_since.elapsed() < IDLE_TIMEOUT && idle.sender.is_ready() && !idle.sender.is_closed() { if idle.idle_since.elapsed() < IDLE_TIMEOUT && idle.sender.is_ready() && !idle.sender.is_closed() {
debug!("Pool hit (h1): {}:{}", key.host, key.port); // H1 pool hit — no logging on hot path
return Some(idle.sender); return Some(idle.sender);
} }
// Stale or closed — drop it // Stale or closed — drop it
@@ -120,42 +126,51 @@ impl ConnectionPool {
let pooled = entry.value(); let pooled = entry.value();
let age = pooled.created_at.elapsed(); let age = pooled.created_at.elapsed();
// Check if the h2 connection is still alive and not too old
if pooled.sender.is_closed() || age >= MAX_H2_AGE { if pooled.sender.is_closed() || age >= MAX_H2_AGE {
let reason = if pooled.sender.is_closed() { "closed" } else { "max_age" };
debug!("Pool evict (h2): {}:{} (reason={}, age={:.1}s)", key.host, key.port, reason, age.as_secs_f64());
drop(entry); drop(entry);
self.h2_pool.remove(key); self.h2_pool.remove(key);
return None; return None;
} }
if pooled.sender.is_ready() { if pooled.sender.is_ready() {
if age > Duration::from_secs(30) {
warn!("Pool hit (h2): {}:{} — connection age {:.1}s (>30s, may be stale)", key.host, key.port, age.as_secs_f64());
} else {
debug!("Pool hit (h2): {}:{} (age={:.1}s)", key.host, key.port, age.as_secs_f64());
}
return Some((pooled.sender.clone(), age)); return Some((pooled.sender.clone(), age));
} }
None None
} }
/// Remove a dead HTTP/2 sender from the pool. /// Remove a dead HTTP/2 sender from the pool (unconditional).
/// Called when `send_request` fails to prevent subsequent requests from reusing the stale sender. /// Called when `send_request` fails to prevent subsequent requests from reusing the stale sender.
pub fn remove_h2(&self, key: &PoolKey) { pub fn remove_h2(&self, key: &PoolKey) {
self.h2_pool.remove(key); self.h2_pool.remove(key);
} }
/// Register an HTTP/2 sender in the pool. Since h2 is multiplexed, /// Remove an HTTP/2 sender ONLY if the current entry has the expected generation.
/// only one sender per key is stored (it's Clone-able). /// This prevents phantom eviction: when multiple connections share the same key,
pub fn register_h2(&self, key: PoolKey, sender: http2::SendRequest<BoxBody<Bytes, hyper::Error>>) { /// an old connection's driver won't accidentally remove a newer connection's entry.
pub fn remove_h2_if_generation(&self, key: &PoolKey, expected_gen: u64) {
if let Some(entry) = self.h2_pool.get(key) {
if entry.value().generation == expected_gen {
drop(entry); // release DashMap ref before remove
self.h2_pool.remove(key);
}
// else: a newer connection replaced ours — don't touch it
}
}
/// Register an HTTP/2 sender in the pool. Returns the generation ID for this entry.
/// The caller should pass this generation to the connection driver so it can use
/// `remove_h2_if_generation` instead of `remove_h2` to avoid phantom eviction.
pub fn register_h2(&self, key: PoolKey, sender: http2::SendRequest<BoxBody<Bytes, hyper::Error>>) -> u64 {
let gen = self.h2_generation.fetch_add(1, Ordering::Relaxed);
if sender.is_closed() { if sender.is_closed() {
return; return gen;
} }
self.h2_pool.insert(key, PooledH2 { self.h2_pool.insert(key, PooledH2 {
sender, sender,
created_at: Instant::now(), created_at: Instant::now(),
generation: gen,
}); });
gen
} }
/// Background eviction loop — runs every EVICTION_INTERVAL to remove stale connections. /// Background eviction loop — runs every EVICTION_INTERVAL to remove stale connections.

View File

@@ -9,10 +9,17 @@ use bytes::Bytes;
use http_body::Frame; use http_body::Frame;
use rustproxy_metrics::MetricsCollector; use rustproxy_metrics::MetricsCollector;
/// Flush accumulated bytes to the metrics collector every 64 KB.
/// This reduces per-frame DashMap shard-locked reads from ~15 to ~1 per 4 frames
/// (assuming typical 16 KB upload frames). The 1 Hz throughput sampler still sees
/// data within one sampling period even at low transfer rates.
const BYTE_FLUSH_THRESHOLD: u64 = 65_536;
/// Wraps any `http_body::Body` and counts data bytes passing through. /// Wraps any `http_body::Body` and counts data bytes passing through.
/// ///
/// Each chunk is reported to the `MetricsCollector` immediately so that /// Bytes are accumulated and flushed to the `MetricsCollector` every
/// the throughput tracker (sampled at 1 Hz) reflects real-time data flow. /// [`BYTE_FLUSH_THRESHOLD`] bytes (and on Drop) so the throughput tracker
/// (sampled at 1 Hz) reflects real-time data flow without per-frame overhead.
/// ///
/// The inner body is pinned on the heap to support `!Unpin` types like `hyper::body::Incoming`. /// The inner body is pinned on the heap to support `!Unpin` types like `hyper::body::Incoming`.
pub struct CountingBody<B> { pub struct CountingBody<B> {
@@ -22,6 +29,8 @@ pub struct CountingBody<B> {
source_ip: Option<String>, source_ip: Option<String>,
/// Whether we count bytes as "in" (request body) or "out" (response body). /// Whether we count bytes as "in" (request body) or "out" (response body).
direction: Direction, direction: Direction,
/// Accumulated bytes not yet flushed to the metrics collector.
pending_bytes: u64,
/// Optional connection-level activity tracker. When set, poll_frame updates this /// Optional connection-level activity tracker. When set, poll_frame updates this
/// to keep the idle watchdog alive during active body streaming (uploads/downloads). /// to keep the idle watchdog alive during active body streaming (uploads/downloads).
connection_activity: Option<Arc<AtomicU64>>, connection_activity: Option<Arc<AtomicU64>>,
@@ -57,6 +66,7 @@ impl<B> CountingBody<B> {
route_id, route_id,
source_ip, source_ip,
direction, direction,
pending_bytes: 0,
connection_activity: None, connection_activity: None,
activity_start: None, activity_start: None,
active_requests: None, active_requests: None,
@@ -81,14 +91,19 @@ impl<B> CountingBody<B> {
self self
} }
/// Report a chunk of bytes immediately to the metrics collector. /// Flush accumulated bytes to the metrics collector.
#[inline] #[inline]
fn report_chunk(&self, len: u64) { fn flush_pending(&mut self) {
if self.pending_bytes == 0 {
return;
}
let bytes = self.pending_bytes;
self.pending_bytes = 0;
let route_id = self.route_id.as_deref(); let route_id = self.route_id.as_deref();
let source_ip = self.source_ip.as_deref(); let source_ip = self.source_ip.as_deref();
match self.direction { match self.direction {
Direction::In => self.metrics.record_bytes(len, 0, route_id, source_ip), Direction::In => self.metrics.record_bytes(bytes, 0, route_id, source_ip),
Direction::Out => self.metrics.record_bytes(0, len, route_id, source_ip), Direction::Out => self.metrics.record_bytes(0, bytes, route_id, source_ip),
} }
} }
} }
@@ -113,9 +128,12 @@ where
Poll::Ready(Some(Ok(frame))) => { Poll::Ready(Some(Ok(frame))) => {
if let Some(data) = frame.data_ref() { if let Some(data) = frame.data_ref() {
let len = data.len() as u64; let len = data.len() as u64;
// Report bytes immediately so the 1 Hz throughput sampler sees them this.pending_bytes += len;
this.report_chunk(len); if this.pending_bytes >= BYTE_FLUSH_THRESHOLD {
// Keep the connection-level idle watchdog alive during body streaming this.flush_pending();
}
// Keep the connection-level idle watchdog alive on every frame
// (this is just one atomic store — cheap enough per-frame)
if let (Some(activity), Some(start)) = (&this.connection_activity, &this.activity_start) { if let (Some(activity), Some(start)) = (&this.connection_activity, &this.activity_start) {
activity.store(start.elapsed().as_millis() as u64, Ordering::Relaxed); activity.store(start.elapsed().as_millis() as u64, Ordering::Relaxed);
} }
@@ -123,7 +141,11 @@ where
Poll::Ready(Some(Ok(frame))) Poll::Ready(Some(Ok(frame)))
} }
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))), Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
Poll::Ready(None) => Poll::Ready(None), Poll::Ready(None) => {
// End of stream — flush any remaining bytes
this.flush_pending();
Poll::Ready(None)
}
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,
} }
} }
@@ -139,6 +161,8 @@ where
impl<B> Drop for CountingBody<B> { impl<B> Drop for CountingBody<B> {
fn drop(&mut self) { fn drop(&mut self) {
// Flush any remaining accumulated bytes so totals stay accurate
self.flush_pending();
// Decrement the active-request counter so the HTTP idle watchdog // Decrement the active-request counter so the HTTP idle watchdog
// knows this response body is no longer streaming. // knows this response body is no longer streaming.
if let Some(ref counter) = self.active_requests { if let Some(ref counter) = self.active_requests {

View File

@@ -109,6 +109,24 @@ impl tokio::io::AsyncWrite for BackendStream {
} }
} }
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[std::io::IoSlice<'_>],
) -> Poll<std::io::Result<usize>> {
match self.get_mut() {
BackendStream::Plain(s) => Pin::new(s).poll_write_vectored(cx, bufs),
BackendStream::Tls(s) => Pin::new(s).poll_write_vectored(cx, bufs),
}
}
fn is_write_vectored(&self) -> bool {
match self {
BackendStream::Plain(s) => s.is_write_vectored(),
BackendStream::Tls(s) => s.is_write_vectored(),
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> { fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
match self.get_mut() { match self.get_mut() {
BackendStream::Plain(s) => Pin::new(s).poll_flush(cx), BackendStream::Plain(s) => Pin::new(s).poll_flush(cx),
@@ -659,11 +677,9 @@ impl HttpProxyService {
h2: use_h2, h2: use_h2,
}; };
// H2 pool checkout with async readiness validation. // H2 pool checkout — reuse pooled connections for all requests.
// checkout_h2 does synchronous is_closed()/is_ready() checks, but these // The h2 crate properly replenishes connection-level flow control
// reflect cached state — the H2 connection driver (a separate tokio task) // windows via release_capacity() as data is consumed.
// may not have processed a pending GOAWAY/RST yet. The ready().await
// forces the runtime to yield, giving the driver a chance to detect failures.
if use_h2 { if use_h2 {
if let Some((mut sender, age)) = self.connection_pool.checkout_h2(&pool_key) { if let Some((mut sender, age)) = self.connection_pool.checkout_h2(&pool_key) {
match tokio::time::timeout( match tokio::time::timeout(
@@ -1001,24 +1017,32 @@ impl HttpProxyService {
} }
}; };
// Spawn the H2 connection driver; proactively evict from pool on exit // Shared generation ID: driver reads it after registration sets it.
// so the next request gets a fresh connection instead of a dead sender. // Uses u64::MAX as sentinel for "not yet registered" (driver waits/skips eviction).
let gen_holder = Arc::new(std::sync::atomic::AtomicU64::new(u64::MAX));
// Spawn the H2 connection driver; evict from pool on exit using generation-tagged
// removal to prevent phantom eviction when multiple connections share the same key.
{ {
let pool = Arc::clone(&self.connection_pool); let pool = Arc::clone(&self.connection_pool);
let key = pool_key.clone(); let key = pool_key.clone();
let gen = Arc::clone(&gen_holder);
tokio::spawn(async move { tokio::spawn(async move {
if let Err(e) = conn.await { if let Err(e) = conn.await {
warn!("HTTP/2 upstream connection error: {} ({:?})", e, e); warn!("HTTP/2 upstream connection error: {} ({:?})", e, e);
} }
pool.remove_h2(&key); let g = gen.load(std::sync::atomic::Ordering::Relaxed);
if g != u64::MAX {
pool.remove_h2_if_generation(&key, g);
}
}); });
} }
// Clone sender for potential pool registration; register only after first request succeeds
let sender_for_pool = sender.clone(); let sender_for_pool = sender.clone();
let result = self.forward_h2_with_sender(sender, parts, body, upstream_headers, upstream_path, route, route_id, source_ip, Some(pool_key), domain, conn_activity).await; let result = self.forward_h2_with_sender(sender, parts, body, upstream_headers, upstream_path, route, route_id, source_ip, Some(pool_key), domain, conn_activity).await;
if matches!(&result, Ok(ref resp) if resp.status() != StatusCode::BAD_GATEWAY) { if matches!(&result, Ok(ref resp) if resp.status() != StatusCode::BAD_GATEWAY) {
self.connection_pool.register_h2(pool_key.clone(), sender_for_pool); let g = self.connection_pool.register_h2(pool_key.clone(), sender_for_pool);
gen_holder.store(g, std::sync::atomic::Ordering::Relaxed);
} }
result result
} }
@@ -1153,15 +1177,20 @@ impl HttpProxyService {
} }
}; };
// Spawn the H2 connection driver; proactively evict from pool on exit. // Spawn the H2 connection driver with generation-tagged eviction.
let gen_holder = Arc::new(std::sync::atomic::AtomicU64::new(u64::MAX));
{ {
let pool = Arc::clone(&self.connection_pool); let pool = Arc::clone(&self.connection_pool);
let key = pool_key.clone(); let key = pool_key.clone();
let gen = Arc::clone(&gen_holder);
tokio::spawn(async move { tokio::spawn(async move {
if let Err(e) = conn.await { if let Err(e) = conn.await {
warn!("H2 retry: upstream connection error: {} ({:?})", e, e); warn!("H2 retry: upstream connection error: {} ({:?})", e, e);
} }
pool.remove_h2(&key); let g = gen.load(std::sync::atomic::Ordering::Relaxed);
if g != u64::MAX {
pool.remove_h2_if_generation(&key, g);
}
}); });
} }
@@ -1189,7 +1218,8 @@ impl HttpProxyService {
match sender.send_request(upstream_req).await { match sender.send_request(upstream_req).await {
Ok(resp) => { Ok(resp) => {
// Register in pool only after request succeeds // Register in pool only after request succeeds
self.connection_pool.register_h2(pool_key.clone(), sender); let g = self.connection_pool.register_h2(pool_key.clone(), sender);
gen_holder.store(g, std::sync::atomic::Ordering::Relaxed);
let result = self.build_streaming_response(resp, route, route_id, source_ip, conn_activity).await; let result = self.build_streaming_response(resp, route, route_id, source_ip, conn_activity).await;
// Close the fresh backend connection (opened above) // Close the fresh backend connection (opened above)
self.metrics.backend_connection_closed(&backend_key); self.metrics.backend_connection_closed(&backend_key);
@@ -1282,15 +1312,20 @@ impl HttpProxyService {
} }
} }
Ok(Ok((mut sender, conn))) => { Ok(Ok((mut sender, conn))) => {
// Spawn the H2 connection driver; proactively evict from pool on exit. // Spawn the H2 connection driver with generation-tagged eviction.
let gen_holder = Arc::new(std::sync::atomic::AtomicU64::new(u64::MAX));
{ {
let pool = Arc::clone(&self.connection_pool); let pool = Arc::clone(&self.connection_pool);
let key = pool_key.clone(); let key = pool_key.clone();
let gen = Arc::clone(&gen_holder);
tokio::spawn(async move { tokio::spawn(async move {
if let Err(e) = conn.await { if let Err(e) = conn.await {
warn!("HTTP/2 upstream connection error: {} ({:?})", e, e); warn!("HTTP/2 upstream connection error: {} ({:?})", e, e);
} }
pool.remove_h2(&key); let g = gen.load(std::sync::atomic::Ordering::Relaxed);
if g != u64::MAX {
pool.remove_h2_if_generation(&key, g);
}
}); });
} }
@@ -1331,28 +1366,24 @@ impl HttpProxyService {
match sender.send_request(upstream_req).await { match sender.send_request(upstream_req).await {
Ok(upstream_response) => { Ok(upstream_response) => {
// H2 works! Register sender in pool for multiplexed reuse let g = self.connection_pool.register_h2(pool_key.clone(), sender);
self.connection_pool.register_h2(pool_key.clone(), sender); gen_holder.store(g, std::sync::atomic::Ordering::Relaxed);
self.build_streaming_response(upstream_response, route, route_id, source_ip, conn_activity).await self.build_streaming_response(upstream_response, route, route_id, source_ip, conn_activity).await
} }
Err(e) => { Err(e) => {
// H2 request failed — backend advertises h2 via ALPN but doesn't // H2 request failed on a stream level (e.g. RST_STREAM PROTOCOL_ERROR).
// actually speak it. Update cache so future requests use H1. // The H2 handshake succeeded, so the backend genuinely speaks H2 — don't
// poison the protocol cache. Only handshake-level failures (below) should
// downgrade the cache to H1.
let bk = format!("{}:{}", upstream.host, upstream.port); let bk = format!("{}:{}", upstream.host, upstream.port);
warn!( debug!(
backend = %bk, backend = %bk,
domain = %domain, domain = %domain,
error = %e, error = %e,
error_debug = ?e, error_debug = ?e,
"Auto-detect: H2 request failed, falling back to H1" "H2 stream error, retrying this request as H1"
); );
self.metrics.backend_h2_failure(&bk); self.metrics.backend_h2_failure(&bk);
let cache_key = crate::protocol_cache::ProtocolCacheKey {
host: upstream.host.clone(),
port: upstream.port,
requested_host: requested_host.clone(),
};
self.protocol_cache.insert(cache_key, crate::protocol_cache::DetectedProtocol::H1);
// Retry as H1 for bodyless requests; return 502 for requests with bodies // Retry as H1 for bodyless requests; return 502 for requests with bodies
if let Some((method, headers)) = retry_state { if let Some((method, headers)) = retry_state {

View File

@@ -50,6 +50,18 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> AsyncWrite for Shutdown
Pin::new(self.get_mut().inner.as_mut().unwrap()).poll_write(cx, buf) Pin::new(self.get_mut().inner.as_mut().unwrap()).poll_write(cx, buf)
} }
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[io::IoSlice<'_>],
) -> Poll<io::Result<usize>> {
Pin::new(self.get_mut().inner.as_mut().unwrap()).poll_write_vectored(cx, bufs)
}
fn is_write_vectored(&self) -> bool {
self.inner.as_ref().unwrap().is_write_vectored()
}
fn poll_flush( fn poll_flush(
self: Pin<&mut Self>, self: Pin<&mut Self>,
cx: &mut Context<'_>, cx: &mut Context<'_>,

View File

@@ -259,40 +259,49 @@ impl MetricsCollector {
/// Called per-chunk in the TCP copy loop. Only touches AtomicU64 counters — /// Called per-chunk in the TCP copy loop. Only touches AtomicU64 counters —
/// no Mutex is taken. The throughput trackers are fed during `sample_all()`. /// no Mutex is taken. The throughput trackers are fed during `sample_all()`.
pub fn record_bytes(&self, bytes_in: u64, bytes_out: u64, route_id: Option<&str>, source_ip: Option<&str>) { pub fn record_bytes(&self, bytes_in: u64, bytes_out: u64, route_id: Option<&str>, source_ip: Option<&str>) {
self.total_bytes_in.fetch_add(bytes_in, Ordering::Relaxed); // Short-circuit: only touch counters for the direction that has data.
self.total_bytes_out.fetch_add(bytes_out, Ordering::Relaxed); // CountingBody always calls with one direction zero — skipping the zero
// direction avoids ~50% of DashMap shard-locked reads per call.
// Accumulate into lock-free pending throughput counters if bytes_in > 0 {
self.global_pending_tp_in.fetch_add(bytes_in, Ordering::Relaxed); self.total_bytes_in.fetch_add(bytes_in, Ordering::Relaxed);
self.global_pending_tp_out.fetch_add(bytes_out, Ordering::Relaxed); self.global_pending_tp_in.fetch_add(bytes_in, Ordering::Relaxed);
}
if bytes_out > 0 {
self.total_bytes_out.fetch_add(bytes_out, Ordering::Relaxed);
self.global_pending_tp_out.fetch_add(bytes_out, Ordering::Relaxed);
}
// Per-route tracking: use get() first (zero-alloc fast path for existing entries), // Per-route tracking: use get() first (zero-alloc fast path for existing entries),
// fall back to entry() with to_string() only on the rare first-chunk miss. // fall back to entry() with to_string() only on the rare first-chunk miss.
if let Some(route_id) = route_id { if let Some(route_id) = route_id {
if let Some(counter) = self.route_bytes_in.get(route_id) { if bytes_in > 0 {
counter.fetch_add(bytes_in, Ordering::Relaxed); if let Some(counter) = self.route_bytes_in.get(route_id) {
} else { counter.fetch_add(bytes_in, Ordering::Relaxed);
self.route_bytes_in.entry(route_id.to_string()) } else {
.or_insert_with(|| AtomicU64::new(0)) self.route_bytes_in.entry(route_id.to_string())
.fetch_add(bytes_in, Ordering::Relaxed); .or_insert_with(|| AtomicU64::new(0))
.fetch_add(bytes_in, Ordering::Relaxed);
}
} }
if let Some(counter) = self.route_bytes_out.get(route_id) { if bytes_out > 0 {
counter.fetch_add(bytes_out, Ordering::Relaxed); if let Some(counter) = self.route_bytes_out.get(route_id) {
} else { counter.fetch_add(bytes_out, Ordering::Relaxed);
self.route_bytes_out.entry(route_id.to_string()) } else {
.or_insert_with(|| AtomicU64::new(0)) self.route_bytes_out.entry(route_id.to_string())
.fetch_add(bytes_out, Ordering::Relaxed); .or_insert_with(|| AtomicU64::new(0))
.fetch_add(bytes_out, Ordering::Relaxed);
}
} }
// Accumulate into per-route pending throughput counters (lock-free) // Accumulate into per-route pending throughput counters (lock-free)
if let Some(entry) = self.route_pending_tp.get(route_id) { if let Some(entry) = self.route_pending_tp.get(route_id) {
entry.0.fetch_add(bytes_in, Ordering::Relaxed); if bytes_in > 0 { entry.0.fetch_add(bytes_in, Ordering::Relaxed); }
entry.1.fetch_add(bytes_out, Ordering::Relaxed); if bytes_out > 0 { entry.1.fetch_add(bytes_out, Ordering::Relaxed); }
} else { } else {
let entry = self.route_pending_tp.entry(route_id.to_string()) let entry = self.route_pending_tp.entry(route_id.to_string())
.or_insert_with(|| (AtomicU64::new(0), AtomicU64::new(0))); .or_insert_with(|| (AtomicU64::new(0), AtomicU64::new(0)));
entry.0.fetch_add(bytes_in, Ordering::Relaxed); if bytes_in > 0 { entry.0.fetch_add(bytes_in, Ordering::Relaxed); }
entry.1.fetch_add(bytes_out, Ordering::Relaxed); if bytes_out > 0 { entry.1.fetch_add(bytes_out, Ordering::Relaxed); }
} }
} }
@@ -302,30 +311,34 @@ impl MetricsCollector {
// This prevents orphaned entries when record_bytes races with // This prevents orphaned entries when record_bytes races with
// connection_closed (which evicts all per-IP data on last close). // connection_closed (which evicts all per-IP data on last close).
if self.ip_connections.contains_key(ip) { if self.ip_connections.contains_key(ip) {
if let Some(counter) = self.ip_bytes_in.get(ip) { if bytes_in > 0 {
counter.fetch_add(bytes_in, Ordering::Relaxed); if let Some(counter) = self.ip_bytes_in.get(ip) {
} else { counter.fetch_add(bytes_in, Ordering::Relaxed);
self.ip_bytes_in.entry(ip.to_string()) } else {
.or_insert_with(|| AtomicU64::new(0)) self.ip_bytes_in.entry(ip.to_string())
.fetch_add(bytes_in, Ordering::Relaxed); .or_insert_with(|| AtomicU64::new(0))
.fetch_add(bytes_in, Ordering::Relaxed);
}
} }
if let Some(counter) = self.ip_bytes_out.get(ip) { if bytes_out > 0 {
counter.fetch_add(bytes_out, Ordering::Relaxed); if let Some(counter) = self.ip_bytes_out.get(ip) {
} else { counter.fetch_add(bytes_out, Ordering::Relaxed);
self.ip_bytes_out.entry(ip.to_string()) } else {
.or_insert_with(|| AtomicU64::new(0)) self.ip_bytes_out.entry(ip.to_string())
.fetch_add(bytes_out, Ordering::Relaxed); .or_insert_with(|| AtomicU64::new(0))
.fetch_add(bytes_out, Ordering::Relaxed);
}
} }
// Accumulate into per-IP pending throughput counters (lock-free) // Accumulate into per-IP pending throughput counters (lock-free)
if let Some(entry) = self.ip_pending_tp.get(ip) { if let Some(entry) = self.ip_pending_tp.get(ip) {
entry.0.fetch_add(bytes_in, Ordering::Relaxed); if bytes_in > 0 { entry.0.fetch_add(bytes_in, Ordering::Relaxed); }
entry.1.fetch_add(bytes_out, Ordering::Relaxed); if bytes_out > 0 { entry.1.fetch_add(bytes_out, Ordering::Relaxed); }
} else { } else {
let entry = self.ip_pending_tp.entry(ip.to_string()) let entry = self.ip_pending_tp.entry(ip.to_string())
.or_insert_with(|| (AtomicU64::new(0), AtomicU64::new(0))); .or_insert_with(|| (AtomicU64::new(0), AtomicU64::new(0)));
entry.0.fetch_add(bytes_in, Ordering::Relaxed); if bytes_in > 0 { entry.0.fetch_add(bytes_in, Ordering::Relaxed); }
entry.1.fetch_add(bytes_out, Ordering::Relaxed); if bytes_out > 0 { entry.1.fetch_add(bytes_out, Ordering::Relaxed); }
} }
} }
} }

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartproxy', name: '@push.rocks/smartproxy',
version: '25.11.11', version: '25.11.23',
description: 'A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.' description: 'A powerful proxy package with unified route-based configuration for high traffic management. Features include SSL/TLS support, flexible routing patterns, WebSocket handling, advanced security options, and automatic ACME certificate management.'
} }