Skip to content

Add HTTP/1.1 persistent connections with connection-scoped buffering and diagnostics - #204

Merged
MisterVVP merged 7 commits into
mainfrom
dev-implement-persistent-http-connections
Aug 11, 2026
Merged

MisterVVP merged 7 commits into
mainfrom
dev-implement-persistent-http-connections

Conversation

@MisterVVP

@MisterVVP MisterVVP commented Aug 11, 2026 •

Copy link
Copy Markdown
Owner

Closes #203

Summary

This PR removes the per-request HTTP connection churn that was limiting HTTP+JSON and JSON-RPC wire scalability.

It keeps the existing HTTP/1.1 transport model, but adds persistent connection support so ordinary unary requests can reuse the same TCP connection instead of paying a new accept/close cycle for every operation.

The implementation also adds connection-scoped buffering, deterministic persistence coverage, connection-reuse diagnostics, Windows-safe shutdown behavior, and request-framing hardening discovered during review.

Why HTTP/1.1 keep-alive

Issue #203 allowed HTTP/1.1 persistence, HTTP/2, or another reusable transport strategy.

HTTP/1.1 keep-alive was selected because it directly addresses the measured structural bottleneck without:

  • adding a large new HTTP/2 dependency;
  • changing public client/server APIs;
  • changing A2A transport semantics;
  • implementing HTTP/2 framing in-house;
  • expanding the scope beyond the connection-lifecycle bottleneck.

The server can now serve multiple sequential unary requests on one connection while preserving existing SSE/streaming behavior.

What changed

Persistent HTTP connections

  • Stop unconditionally forcing Connection: close for reusable unary responses.
  • Honor explicit Connection: close.
  • Keep streaming responses connection-closing under the current HTTP/1.1 model.
  • Allow multiple requests to be processed sequentially on one accepted socket.
  • Preserve the existing two-argument HttpAdapter::WriteResponse(...) API while adding explicit connection-close control.

Connection-scoped request buffering

HttpConnectionState now retains bytes that were read beyond the current request boundary.

This allows the adapter to correctly handle:

  • multiple requests arriving in one read;
  • back-to-back body-bearing requests;
  • fragmented requests spanning multiple reads;
  • pipelined buffered carry-over;
  • explicit connection closure;
  • malformed request termination.

Request-framing hardening

The adapter explicitly rejects Transfer-Encoding.

The current raw HTTP adapter frames request bodies using Content-Length; accepting transfer-encoded requests could otherwise leave encoded bytes in the connection buffer and desynchronize subsequent request parsing.

Rejecting unsupported transfer encoding preserves deterministic framing and prevents ambiguous request-stream interpretation.

Server lifecycle and portability

  • Reuse one worker for multiple requests on an active connection instead of effectively creating a new socket/thread lifecycle for every unary operation.
  • Keep the signal-owned shutdown flag out of persistent worker-thread control flow.
  • Shut active sockets down during SUT termination so blocked workers exit cleanly.
  • Make Windows listener sockets genuinely nonblocking using Winsock FIONBIO.
  • Handle WSAEWOULDBLOCK / WSAEINTR in the Windows accept loop.
  • Use a Windows process group plus CTRL_BREAK_EVENT for graceful performance-SUT shutdown so shutdown-time diagnostics are emitted consistently.

Performance diagnostics

The performance runner now records:

  • http_coordinate_accepted_connections;
  • http_coordinate_completed_unary_operations;
  • http_coordinate_operations_per_connection.

The denominator counts only connections that actually carried unary work, so readiness probes and streaming-only sockets do not distort unary connection-reuse measurements.

Diagnostics are available in both JSON and CSV reports.

The parser also accepts scientific-notation values emitted by C++ stream formatting.

Focused performance execution

The performance runner supports focused scenario selection, including the case where the selected scenarios contain no wire-capable workload.

This makes targeted transport validation easier without requiring the entire performance matrix.

Connection reuse evidence

Latest in-memory HTTP wire diagnostics:

Concurrency Unary operations Unary-carrying connections Operations / connection
1 27,048 1,015 26.65
4 27,048 1,054 25.66
16 27,048 1,210 22.35
64 27,048 1,834 14.75

The important result is structural: ordinary unary operations no longer require one accepted TCP connection per operation.

At c64, 27,048 unary operations were completed using 1,834 unary-carrying connections.

Performance comparison

Representative in-memory wire throughput at c64 compared with the final PR #202 CI artifact:

Scenario HTTP+JSON #202 HTTP+JSON this PR Δ JSON-RPC #202 JSON-RPC this PR Δ
GetTask_ExistingTask 5,135 ops/s 7,515 ops/s +46.3% 2,682 ops/s 3,314 ops/s +23.6%
ListTasks_WithPagination 1,107 ops/s 1,188 ops/s +7.3% 977 ops/s 1,032 ops/s +5.7%
ListTasks_NoPagination 596 ops/s 626 ops/s +5.0% 529 ops/s 546 ops/s +3.2%
SendMessage_CreateTask 4,383 ops/s 5,860 ops/s +33.7% 2,386 ops/s 2,785 ops/s +16.7%

These values come from separate GitHub-hosted CI executions, so the exact percentages should be treated as directional rather than precise same-machine benchmark deltas.

As an additional control, normalizing HTTP throughput against gRPC within each corresponding run still improves the relative c64 throughput for all four representative scenarios, supporting that the improvement is transport-specific rather than simply a faster runner.

Tail latency is noisier across unrelated hosted-runner executions, so this PR does not claim precise p95/p99 improvements from those cross-run values.

The latest performance matrix completed 512 result rows with zero operation errors.

@MisterVVP MisterVVP added the codex Automatic label added by codex agent. Should not be used label Aug 11, 2026 — with ChatGPT Codex Connector
@MisterVVP MisterVVP added AI agent AI agent contribution and removed codex Automatic label added by codex agent. Should not be used labels Aug 11, 2026
@MisterVVP MisterVVP self-assigned this Aug 11, 2026
@MisterVVP
MisterVVP marked this pull request as draft August 11, 2026 06:09
@MisterVVP MisterVVP added the performance Performance improvement related tasks based on benchmark results. Performance tests changes. label Aug 11, 2026
@MisterVVP

Copy link
Copy Markdown
Owner Author

@codex review this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e77c21fc47

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/sut/tck_sut.cpp Resolved
Comment thread tests/scripts/http_persistence_test.py Resolved
Comment thread tests/scripts/http_persistence_test.py Resolved
Comment thread scripts/performance_runner.py Resolved
Comment thread scripts/performance_runner.py Resolved
Comment thread tests/scripts/http_persistence_test.py Resolved
@MisterVVP

Copy link
Copy Markdown
Owner Author

@codex please review this PR. Do exhaustive review. Please report only actionable remaining issues that should block merge.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 86ffd9aa8c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/server/transport/http_adapter.cpp Resolved
Comment thread tests/sut/tck_sut.cpp Resolved
Comment thread scripts/performance_runner.py Resolved
Comment thread tests/sut/tck_sut.cpp Resolved
Comment thread tests/scripts/http_persistence_test.py Resolved
@MisterVVP
MisterVVP marked this pull request as ready for review August 11, 2026 14:34
@MisterVVP

Copy link
Copy Markdown
Owner Author

@codex review this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f62b66e26c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/server/transport/http_adapter.cpp Resolved
Comment thread scripts/performance_runner.py Resolved
Comment thread scripts/performance_runner.py Resolved
Comment thread tests/scripts/http_persistence_test.py Resolved
@MisterVVP
MisterVVP force-pushed the dev-implement-persistent-http-connections branch from 1023f27 to e2bb73e Compare August 11, 2026 18:26
@MisterVVP
MisterVVP merged commit cf62401 into main Aug 11, 2026
18 checks passed
@MisterVVP
MisterVVP deleted the dev-implement-persistent-http-connections branch August 11, 2026 19:27
MisterVVP added a commit that referenced this pull request Sep 12, 2026
… latency (#223)

## Summary

Closes #209.

This PR reduces startup, first-event, completion, and cancellation
overhead in the existing synchronous/public streaming APIs, with a
particular focus on HTTP+JSON and JSON-RPC SSE streaming under
concurrency.

The default HTTP streaming path now uses a bounded, process-wide
event-driven libcurl architecture instead of creating one OS worker
thread per stream. Network progress is separated from application
callback execution while preserving the existing `A2AClient`,
`StreamHandle`, transport, observer, and server-streaming APIs.

## What changed

### Shared event-driven HTTP execution

Default HTTP+JSON and JSON-RPC streaming now use shared `CURLM` reactors
instead of one blocking libcurl worker per stream.

- The process-wide reactor pool is bounded to four reactors.
- Linux uses `epoll`, `eventfd`, and `timerfd`.
- Other platforms use libcurl's portable multi-event path.
- Unary and streaming requests participate in the same low-level reactor
infrastructure.
- Reusable request/stream slots retain reactor affinity for
connection-cache locality.

### Bounded callback dispatch

Application observer callbacks run away from the network reactor on a
bounded shared executor.

- Per-stream callback ordering remains serialized.
- Callback backlog is bounded by task count and pending bytes.
- Slow observers do not block network progress.
- Cancellation, shutdown, and transport finalization do not depend on
unrelated callback workers becoming available.

### Streaming latency and allocation reductions

The streaming path also reduces avoidable work by:

- delivering finite HTTP streams incrementally;
- reducing SSE framing/parsing temporary allocations and copies;
- avoiding unnecessary JSON/protobuf round trips in JSON-RPC streaming
where possible;
- preserving response validation and protocol error mapping;
- reusing HTTP connections for finite streams where safe.

### Cancellation and lifecycle correctness

The implementation now covers and tests:

- prompt cancellation of idle transfers;
- cancellation racing remote completion;
- cancellation from inside callbacks;
- cross-stream cancellation without cyclic waits;
- shutdown racing stream startup;
- bounded reactor creation during teardown;
- safe reactor destruction off the reactor thread;
- callback-executor construction failure;
- reactor-thread construction failure;
- safe copied/moved low-level HTTP client ownership;
- `A2AClient::Destroy()` with active streams;
- synchronous cancellation-predicate serialization;
- suppression of new callbacks after shutdown.

Custom injected synchronous stream requesters retain their compatibility
path and continue using `StreamHandle`-owned workers.

### Diagnostics and performance tooling

The performance suite now includes wire-level measurements and
diagnostics for:

- finite-stream first-event latency;
- finite-stream completion latency;
- task-subscription first-event latency;
- shared-client streaming;
- local cancellation latency;
- finite-stream connection reuse;
- client/server subscription phase attribution.

Diagnostics are reset at the measurement boundary so warmup/setup
traffic is not attributed to measured rows.

### Implementation cleanup

The low-level HTTP implementation was split by responsibility:

- `src/http/http_client.cpp` — public client lifecycle and unary
orchestration;
- `src/http/http_client_curl.cpp` — libcurl request/response
configuration and parsing;
- `src/http/http_client_streaming.cpp` — streaming state, callback
dispatch, cancellation, and completion;
- `src/http/curl_stream_reactor.cpp` — event-driven reactor
implementation;
- `src/http/http_client_internal.h` — private cross-translation-unit
implementation types.

This reduces `http_client.cpp` from roughly 52 KB to roughly 9 KB
without changing public behavior.

## Performance

### Comparison methodology

For acceptance validation, this table compares complete normal-CI
artifacts produced with the same performance configuration.

Baseline:

- PR #204 head: `e2bb73e18a6910bb1c94213872f7a0f74b160ffd`
- CI run: `31522741927`
- artifact: `perf-artifacts-normal` / `9114133095`

PR #223 final head:

- head: `1fa92d8ffb7f0965db7696c56df88d52ee0eb567`
- CI run: `34695805370`
- artifact: `perf-artifacts-normal` / `10298841665`

Configuration:

```text
A2A_PERF_TRANSPORTS=grpc,jsonrpc,http_json
A2A_PERF_STORE_BACKENDS=inmemory,postgres
A2A_PERF_REQUESTS=1000
A2A_PERF_POSTGRES_POOL_SIZES=64
A2A_PERF_CONCURRENCY=1,4,16,64
A2A_PERF_WARMUP_SECONDS=0
```

The table below shows the acceptance-critical in-memory, wire-level,
concurrency-64 coordinate.

| Scenario | Transport | First-event p95 | Completion p95 | Throughput |
|---|---|---:|---:|---:|
| Finite stream | gRPC | 8.923 → 7.049 ms (**-21.0%**) | 12.256 → 9.617
ms (**-21.5%**) | 6,464 → 8,495 ops/s (**+31.4%**) |
| Finite stream | HTTP+JSON | 21.219 → 15.105 ms (**-28.8%**) | 21.438 →
15.816 ms (**-26.2%**) | 4,687 → 6,731 ops/s (**+43.6%**) |
| Finite stream | JSON-RPC | 34.981 → 19.290 ms (**-44.9%**) | 35.099 →
19.964 ms (**-43.1%**) | 2,939 → 5,989 ops/s (**+103.8%**) |
| Subscribe | gRPC | 10.295 → 8.083 ms (**-21.5%**) | 21.807 → 19.692 ms
(**-9.7%**) | 2,412 → 2,850 ops/s (**+18.2%**) |
| Subscribe | HTTP+JSON | 17.025 → 12.864 ms (**-24.4%**) | 43.448 →
30.379 ms (**-30.1%**) | 1,717 → 2,098 ops/s (**+22.2%**) |
| Subscribe | JSON-RPC | 44.445 → 26.470 ms (**-40.4%**) | 83.695 →
60.793 ms (**-27.4%**) | 890 → 1,090 ops/s (**+22.4%**) |

All target rows completed with zero operation errors.

The final normal performance artifact contains 568 result rows / 568,000
measured operations with zero operation errors.

The PostgreSQL c64 verification also improved the target HTTP streaming
coordinates versus the same PR #204 artifact:

- finite-stream first-event p95: HTTP+JSON **-17.1%**, JSON-RPC
**-16.7%**;
- finite-stream completion p95: HTTP+JSON **-15.0%**, JSON-RPC
**-15.9%**;
- subscription first-event p95: HTTP+JSON **-54.1%**, JSON-RPC
**-57.1%**.

> Note: issue #209's context table records a different baseline sample.
The acceptance criteria call for equivalent runs, so the comparison
above uses the complete PR #204 and PR #223 normal-CI artifacts
generated with the same configuration.

## Validation

Current head `1fa92d8ffb7f0965db7696c56df88d52ee0eb567` passes:

- build / unit tests / changed-file clang-tidy;
- formatting checks;
- coverage;
- benchmarks and benchmark thresholds;
- ASan/UBSan;
- Linux CI;
- macOS build;
- Windows/MSVC build;
- Python/C++ cross-SDK interoperability;
- HTTP SSE interoperability;
- normal performance matrix;
- PostgreSQL tail performance validation;
- mandatory TCK conformance for both in-memory and PostgreSQL stores.

The mandatory TCK workflow reports no mandatory requirement failures for
either backend.

## Issue #209 acceptance

The PR meets the issue's functional and performance goals:

- existing synchronous/public APIs remain source-compatible;
- streaming event ordering, cancellation, terminal delivery, and error
behavior remain covered;
- finite-stream c64 p95 improves by more than 15% for both HTTP+JSON and
JSON-RPC in equivalent in-memory runs;
- subscription first-event c64 p95 improves by more than 15% for both
HTTP+JSON and JSON-RPC;
- gRPC does not regress and instead improves on the acceptance-critical
coordinates;
- target performance rows report zero operation errors;
- shared/bounded execution has dedicated concurrency, cancellation,
destruction, and worker-growth coverage;
- streaming interoperability passes;
- mandatory TCK remains conformant for executed/non-skipped mandatory
requirements.

The separate opt-in asynchronous API tracked by #169 remains out of
scope.

---------

Co-authored-by: Vladimir Pavlov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI agent AI agent contribution performance Performance improvement related tasks based on benchmark results. Performance tests changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance]: Add persistent HTTP connections and reduce per-request connection churn

1 participant