Compare commits

..

6 Commits

Author SHA1 Message Date
0930f7e10c v25.11.4
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-15 21:44:32 +00:00
aa9e6dfd94 fix(rustproxy-http): report streamed HTTP and WebSocket bytes per chunk for real-time throughput metrics 2026-03-15 21:44:32 +00:00
211d5cf835 v25.11.3
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-15 17:00:33 +00:00
2ce1899337 fix(repo): no changes to commit 2026-03-15 17:00:33 +00:00
2e2ffc4485 v25.11.2
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-15 16:58:41 +00:00
da26816af5 fix(rustproxy-http): avoid reusing HTTP/1 senders during streaming responses and relax HTTP/2 keep-alive timeouts 2026-03-15 16:58:41 +00:00
5 changed files with 53 additions and 45 deletions

View File

@@ -1,5 +1,22 @@
# Changelog # Changelog
## 2026-03-15 - 25.11.4 - fix(rustproxy-http)
report streamed HTTP and WebSocket bytes per chunk for real-time throughput metrics
- Update CountingBody to record bytes immediately on each data frame instead of aggregating until completion or drop
- Record WebSocket tunnel traffic inside both copy loops and remove the final aggregate byte report to keep throughput metrics current
## 2026-03-15 - 25.11.3 - fix(repo)
no changes to commit
## 2026-03-15 - 25.11.2 - fix(rustproxy-http)
avoid reusing HTTP/1 senders during streaming responses and relax HTTP/2 keep-alive timeouts
- Stop returning HTTP/1 senders to the connection pool before upstream response bodies finish streaming to prevent unsafe reuse on active connections.
- Increase HTTP/2 keep-alive timeout from 5 seconds to 30 seconds in proxy connection builders to better support longer-lived backend streams.
- Improves reliability for large streaming payloads and backend fallback request handling.
## 2026-03-15 - 25.11.1 - fix(rustproxy-http) ## 2026-03-15 - 25.11.1 - fix(rustproxy-http)
keep connection idle tracking alive during streaming and tune HTTP/2 connection lifetimes keep connection idle tracking alive during streaming and tune HTTP/2 connection lifetimes

View File

@@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartproxy", "name": "@push.rocks/smartproxy",
"version": "25.11.1", "version": "25.11.4",
"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

@@ -11,20 +11,17 @@ use rustproxy_metrics::MetricsCollector;
/// Wraps any `http_body::Body` and counts data bytes passing through. /// Wraps any `http_body::Body` and counts data bytes passing through.
/// ///
/// When the body is fully consumed or dropped, accumulated byte counts /// Each chunk is reported to the `MetricsCollector` immediately so that
/// are reported to the `MetricsCollector`. /// the throughput tracker (sampled at 1 Hz) reflects real-time data flow.
/// ///
/// 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> {
inner: Pin<Box<B>>, inner: Pin<Box<B>>,
counted_bytes: AtomicU64,
metrics: Arc<MetricsCollector>, metrics: Arc<MetricsCollector>,
route_id: Option<String>, route_id: Option<String>,
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,
/// Whether we've already reported the bytes (to avoid double-reporting on drop).
reported: bool,
/// 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>>,
@@ -52,12 +49,10 @@ impl<B> CountingBody<B> {
) -> Self { ) -> Self {
Self { Self {
inner: Box::pin(inner), inner: Box::pin(inner),
counted_bytes: AtomicU64::new(0),
metrics, metrics,
route_id, route_id,
source_ip, source_ip,
direction, direction,
reported: false,
connection_activity: None, connection_activity: None,
activity_start: None, activity_start: None,
} }
@@ -72,33 +67,18 @@ impl<B> CountingBody<B> {
self self
} }
/// Report accumulated bytes to the metrics collector. /// Report a chunk of bytes immediately to the metrics collector.
fn report(&mut self) { #[inline]
if self.reported { fn report_chunk(&self, len: u64) {
return;
}
self.reported = true;
let bytes = self.counted_bytes.load(Ordering::Relaxed);
if bytes == 0 {
return;
}
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(bytes, 0, route_id, source_ip), Direction::In => self.metrics.record_bytes(len, 0, route_id, source_ip),
Direction::Out => self.metrics.record_bytes(0, bytes, route_id, source_ip), Direction::Out => self.metrics.record_bytes(0, len, route_id, source_ip),
} }
} }
} }
impl<B> Drop for CountingBody<B> {
fn drop(&mut self) {
self.report();
}
}
// CountingBody is Unpin because inner is Pin<Box<B>> (always Unpin). // CountingBody is Unpin because inner is Pin<Box<B>> (always Unpin).
impl<B> Unpin for CountingBody<B> {} impl<B> Unpin for CountingBody<B> {}
@@ -118,7 +98,9 @@ where
match this.inner.as_mut().poll_frame(cx) { match this.inner.as_mut().poll_frame(cx) {
Poll::Ready(Some(Ok(frame))) => { Poll::Ready(Some(Ok(frame))) => {
if let Some(data) = frame.data_ref() { if let Some(data) = frame.data_ref() {
this.counted_bytes.fetch_add(data.len() as u64, Ordering::Relaxed); let len = data.len() as u64;
// Report bytes immediately so the 1 Hz throughput sampler sees them
this.report_chunk(len);
// Keep the connection-level idle watchdog alive during body streaming // Keep the connection-level idle watchdog alive during body streaming
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);
@@ -127,11 +109,7 @@ 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),
// Body is fully consumed — report now
this.report();
Poll::Ready(None)
}
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,
} }
} }

View File

@@ -910,8 +910,15 @@ impl HttpProxyService {
} }
}; };
// Return sender to pool (body streams lazily, sender is reusable once response head is received) // Note: we do NOT return the sender to the pool here because the response body
self.connection_pool.checkin_h1(pool_key.clone(), sender); // hasn't been fully streamed yet. Pooling a sender while its response body is still
// in-flight risks another request being dispatched on the same connection if is_ready()
// momentarily returns true between chunks. The sender is dropped after this scope,
// and the backend connection remains alive via the spawned conn driver task until
// the response body finishes streaming.
// For small/empty responses, the sender could theoretically be reused, but the safety
// of large streaming responses (e.g. 352MB Docker layers) takes priority.
drop(sender);
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
} }
@@ -939,7 +946,7 @@ impl HttpProxyService {
h2_builder h2_builder
.timer(hyper_util::rt::TokioTimer::new()) .timer(hyper_util::rt::TokioTimer::new())
.keep_alive_interval(std::time::Duration::from_secs(10)) .keep_alive_interval(std::time::Duration::from_secs(10))
.keep_alive_timeout(std::time::Duration::from_secs(5)) .keep_alive_timeout(std::time::Duration::from_secs(30))
.initial_stream_window_size(2 * 1024 * 1024) .initial_stream_window_size(2 * 1024 * 1024)
.initial_connection_window_size(16 * 1024 * 1024); .initial_connection_window_size(16 * 1024 * 1024);
let (sender, conn): ( let (sender, conn): (
@@ -1082,7 +1089,7 @@ impl HttpProxyService {
h2_builder h2_builder
.timer(hyper_util::rt::TokioTimer::new()) .timer(hyper_util::rt::TokioTimer::new())
.keep_alive_interval(std::time::Duration::from_secs(10)) .keep_alive_interval(std::time::Duration::from_secs(10))
.keep_alive_timeout(std::time::Duration::from_secs(5)) .keep_alive_timeout(std::time::Duration::from_secs(30))
.initial_stream_window_size(2 * 1024 * 1024) .initial_stream_window_size(2 * 1024 * 1024)
.initial_connection_window_size(16 * 1024 * 1024); .initial_connection_window_size(16 * 1024 * 1024);
let (mut sender, conn): ( let (mut sender, conn): (
@@ -1178,7 +1185,7 @@ impl HttpProxyService {
h2_builder h2_builder
.timer(hyper_util::rt::TokioTimer::new()) .timer(hyper_util::rt::TokioTimer::new())
.keep_alive_interval(std::time::Duration::from_secs(10)) .keep_alive_interval(std::time::Duration::from_secs(10))
.keep_alive_timeout(std::time::Duration::from_secs(5)) .keep_alive_timeout(std::time::Duration::from_secs(30))
.initial_stream_window_size(2 * 1024 * 1024) .initial_stream_window_size(2 * 1024 * 1024)
.initial_connection_window_size(16 * 1024 * 1024); .initial_connection_window_size(16 * 1024 * 1024);
let handshake_result = tokio::time::timeout( let handshake_result = tokio::time::timeout(
@@ -1425,8 +1432,8 @@ impl HttpProxyService {
} }
}; };
// Return sender to pool for keep-alive reuse // Don't pool the sender while response body is still streaming (same safety as forward_h1_with_sender)
self.connection_pool.checkin_h1(pool_key.clone(), sender); drop(sender);
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
} }
@@ -1912,6 +1919,9 @@ impl HttpProxyService {
let conn_act_u2c = conn_activity.as_ref().map(|ca| (Arc::clone(&ca.last_activity), ca.start)); let conn_act_u2c = conn_activity.as_ref().map(|ca| (Arc::clone(&ca.last_activity), ca.start));
let la1 = Arc::clone(&last_activity); let la1 = Arc::clone(&last_activity);
let metrics_c2u = Arc::clone(&metrics);
let route_c2u = route_id_owned.clone();
let ip_c2u = source_ip_owned.clone();
let c2u = tokio::spawn(async move { let c2u = tokio::spawn(async move {
let mut buf = vec![0u8; 65536]; let mut buf = vec![0u8; 65536];
let mut total = 0u64; let mut total = 0u64;
@@ -1924,6 +1934,7 @@ impl HttpProxyService {
break; break;
} }
total += n as u64; total += n as u64;
metrics_c2u.record_bytes(n as u64, 0, route_c2u.as_deref(), Some(&ip_c2u));
la1.store(start.elapsed().as_millis() as u64, Ordering::Relaxed); la1.store(start.elapsed().as_millis() as u64, Ordering::Relaxed);
if let Some((ref ca, ca_start)) = conn_act_c2u { if let Some((ref ca, ca_start)) = conn_act_c2u {
ca.store(ca_start.elapsed().as_millis() as u64, Ordering::Relaxed); ca.store(ca_start.elapsed().as_millis() as u64, Ordering::Relaxed);
@@ -1934,6 +1945,9 @@ impl HttpProxyService {
}); });
let la2 = Arc::clone(&last_activity); let la2 = Arc::clone(&last_activity);
let metrics_u2c = Arc::clone(&metrics);
let route_u2c = route_id_owned.clone();
let ip_u2c = source_ip_owned.clone();
let u2c = tokio::spawn(async move { let u2c = tokio::spawn(async move {
let mut buf = vec![0u8; 65536]; let mut buf = vec![0u8; 65536];
let mut total = 0u64; let mut total = 0u64;
@@ -1946,6 +1960,7 @@ impl HttpProxyService {
break; break;
} }
total += n as u64; total += n as u64;
metrics_u2c.record_bytes(0, n as u64, route_u2c.as_deref(), Some(&ip_u2c));
la2.store(start.elapsed().as_millis() as u64, Ordering::Relaxed); la2.store(start.elapsed().as_millis() as u64, Ordering::Relaxed);
if let Some((ref ca, ca_start)) = conn_act_u2c { if let Some((ref ca, ca_start)) = conn_act_u2c {
ca.store(ca_start.elapsed().as_millis() as u64, Ordering::Relaxed); ca.store(ca_start.elapsed().as_millis() as u64, Ordering::Relaxed);
@@ -2006,9 +2021,7 @@ impl HttpProxyService {
debug!("WebSocket tunnel closed: {} bytes in, {} bytes out", bytes_in, bytes_out); debug!("WebSocket tunnel closed: {} bytes in, {} bytes out", bytes_in, bytes_out);
upstream_selector.connection_ended(&upstream_key_owned); upstream_selector.connection_ended(&upstream_key_owned);
if let Some(ref rid) = route_id_owned { // Bytes already reported per-chunk in the copy loops above
metrics.record_bytes(bytes_in, bytes_out, Some(rid.as_str()), Some(&source_ip_owned));
}
}); });
let body: BoxBody<Bytes, hyper::Error> = BoxBody::new( let body: BoxBody<Bytes, hyper::Error> = BoxBody::new(

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartproxy', name: '@push.rocks/smartproxy',
version: '25.11.1', version: '25.11.4',
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.'
} }