Skip to content

Handle source connections concurrently - #177

Open
ameba23 wants to merge 5 commits into
mainfrom
peg/improve-concurrency
Open

Handle source connections concurrently#177
ameba23 wants to merge 5 commits into
mainfrom
peg/improve-concurrency

Conversation

@ameba23

@ameba23 ameba23 commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

This improves concurrency to proxy-client to proxy-server connections.

On HTTP2 (default) requests run concurrently. proxy-server to target service connections still use HTTP1.1 - since we dont use TLS for these and therefore no protocol negotiation. This means we end up with multiple connections between proxy-server and target (usually the same host), multiplexed over a single attested connection over the wire.

On HTTP1.1, requests remain serialized but this PR adds deadlines, cancellation and reconnects.

Limits are configurable: 64 in-flight requests, 60-second request deadlines, and 60-second response idle timeouts, through CLI flags and ProxyClientOptions.

Closes #118

@ameba23
ameba23 marked this pull request as draft September 11, 2026 15:15
@ameba23
ameba23 marked this pull request as ready for review September 14, 2026 09:06
@ameba23
ameba23 requested a lite review from Copilot September 14, 2026 09:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical request-capacity and cancellation issues, plus a moderate pipelining timeout issue, block approval.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Improves proxy-client forwarding with concurrent HTTP/2 handling, HTTP/1.1 reconnects, deadlines, cancellation, and configurable limits.

Changes:

  • Adds configurable in-flight request limits and timeouts.
  • Implements request-body cancellation and response idle tracking.
  • Adds concurrency, reconnect, timeout tests, CLI options, and documentation.
File summaries
File Summary
src/main.rs Adds client configuration flags.
src/lib.rs Integrates concurrency and reconnect logic. Critical: queued or timed-out requests can retain permits; oversized semaphore limits can panic.
src/http_version.rs Supports the new request body abstraction.
src/client_request/upload.rs Provides cancellable upload handling. Critical: canceled uploads can retain semaphore permits.
src/client_request/tests.rs Tests concurrency, cancellation, timeouts, and streaming.
src/client_request/response_idle.rs Tracks response write activity. Moderate: shared response state can mishandle pipelined responses.
src/client_request/mod.rs Implements forwarding, deadlines, and capacity tracking. Critical: dropped bodies or canceled uploads can retain request slots.
README.md Documents the new configuration options.
Review details
  • Files reviewed: 8/8 changed files
  • Comments generated: 6
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/client_request/mod.rs
Comment on lines +216 to +220
_ = tokio::time::sleep_until(deadline), if !uploaded => {
// Headers may already have reached the caller, so a 504 can no
// longer replace them. Cancel the upload instead.
drop(upload_guard);
return ForwardResult { reconnect: http1 || sender.is_closed(), sender };

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a fix for this in a separate branch, but it ended up being quite a bit of extra code for something debatably quite edge-casey. Im not sure if i want to merge it here.

Comment thread src/client_request/mod.rs
Comment on lines +239 to +246
impl TrackedBody {
fn finish(&mut self) {
self.permit.take();
if let Some(finished) = self.finished.take() {
let _ = finished.send(());
}
}
}
Comment on lines +53 to +56
pub(crate) struct RequestBody {
state: Arc<Mutex<UploadState>>,
permit: Option<Arc<OwnedSemaphorePermit>>,
}
Comment thread src/lib.rs
Comment on lines +368 to +370
pub fn with_request_options(mut self, options: ProxyClientOptions) -> Self {
self.request_slots = Arc::new(Semaphore::new(options.max_in_flight_requests.get()));
self.options = options;
Comment thread src/lib.rs
Comment on lines +718 to +724
requests_tx
.send(PendingRequest {
request: req,
response_tx,
deadline,
permit,
})
Comment on lines +67 to +83
impl ResponseActivity {
/// Starts idle tracking and wraps the response body to observe completion.
pub(crate) fn track(&self, response: ProxyResponse) -> ProxyResponse {
self.0.send_replace(Some(ActiveResponse {
last_write: Instant::now(),
body_finished: false,
}));
response.map(|inner| {
let mut body = IdleBody {
inner,
activity: Some(self.clone()),
};
if body.inner.is_end_stream() {
body.finish();
}
body.boxed()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proxy client serializes requests on a single connection (no concurrency)

2 participants