Skip to content

feat(engine): give each ranged connection its own socket - #4

Merged
troke12 merged 2 commits into
troke12:mainfrom
rigaz29:engine-http1-ranges
Oct 19, 2026
Merged

troke12 merged 2 commits into
troke12:mainfrom
rigaz29:engine-http1-ranges

Conversation

@rigaz29

@rigaz29 rigaz29 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Over HTTP/2, all ranged requests of a segmented download are multiplexed onto one TCP connection, so "N connections" gave one socket's throughput. A download with more than one ranged connection now uses a per-download HTTP/1.1 client, so each connection gets its own socket. A single stream, a server without ranges, or the one-connection fallback (#10) keep the shared client and can still use HTTP/2.

Details

  • segmentTransport clones the shared transport (proxy, pool settings) with HTTP/2 off: ForceAttemptHTTP2=false, an empty TLSNextProto, and ALPN limited to http/1.1. Without the ALPN fix an h2 server picks h2 and every chunk fails (the review's finding).
  • The HTTP/1.1 client goes through the local-network guard (guardLocal) like every other download request. This PR predated the guard; the rebase wraps it. It is rebuilt on the same changes as guardedClient (client or manifest), and its idle connections are closed when a run ends.
  • exchange takes the client to use; guardManifestLocked is shared with guardedClient.

Commits

Test plan

  • go test ./... and go test -race ./internal/engine
  • TestSegmentedDownloadOverHTTP2Server: TLS with EnableHTTP2. The probe uses h2, the segmented requests use HTTP/1.1 on more than one socket, and the file matches. Fails with EOF without the ALPN fix.
  • TestSegmentTransportDisablesHTTP2: ALPN is [http/1.1], including from a nil base.
  • TestSegmentedDownloadUsesHTTP1: the HTTP/1.1 client is wrapped by the local-network guard. Fails when it is not.
  • TestOneConnectionKeepsSharedClient, TestNoRangeKeepsSharedClient

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)

@troke12 troke12 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Good motivation (h2 multiplexes all ranges onto one connection), but as written this breaks segmented HTTPS downloads from HTTP/2 servers (most CDNs):

base.Clone() first runs onceSetNextProtoDefaults on the base; since the base has ForceAttemptHTTP2: true, http2configureTransports sets TLSClientConfig.NextProtos = ["h2", "http/1.1"] (h2_bundle.go). The clone copies that config, so the "HTTP/1-only" transport still offers h2 in ALPN. The server picks h2, TLSNextProto has no "h2" entry, and the transport speaks HTTP/1.1 on an h2 connection → every chunk fails.

Please set t.TLSClientConfig.NextProtos = []string{"http/1.1"} after cloning, and add a TLS test (httptest.NewUnstartedServer with EnableHTTP2 = true + StartTLS) — the current tests use plain HTTP so ALPN never comes up.

Minor: per-download transports never call CloseIdleConnections.


Note for the whole set (#1–#11): thanks a lot for these! They are all branched from 5350f79 independently, so every pair conflicts on CHANGELOG.md and most on download.go. main has also moved (e830f5d adds a stream downloader that touches download.go). Could you rebase onto current main and stack them in this order: #1 → #2 → #10 → #8 → #5/#6/#7/#11? #2, #3 and #10 each add their own pass loop / transferCancel in Start; one shared design would be much easier to merge.

rigaz29 and others added 2 commits October 19, 2026 10:31
Several ranges on one HTTP/2 connection share a single socket. A segmented download now uses HTTP/1.1, one socket per connection. A single stream still uses the shared client.
Review changes on top of the previous commit:

- segmentTransport sets TLSClientConfig.NextProtos to ["http/1.1"].
  base.Clone() first sets up HTTP/2 on base, which puts "h2" in its TLS
  config's ALPN list, and the copy kept it: an HTTP/2 server (most CDNs)
  picked h2, the transport spoke HTTP/1.1 on it, and every chunk failed
  with EOF. TestSegmentedDownloadOverHTTP2Server runs over TLS with
  EnableHTTP2 and fails that way without the fix.
- The per-download HTTP/1.1 client closes its idle connections when a
  run ends; its pool is its own.
- TestSegmentedDownloadUsesHTTP1 checks the client goes through the
  local-network guard (the previous commit, rebased onto main, wraps it
  with guardLocal like guardedClient).

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
@troke12
troke12 force-pushed the engine-http1-ranges branch from 0a4f8e6 to 992b6ea Compare October 19, 2026 03:33
@troke12

troke12 commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Rebased onto current main and applied the review: the segmented transport now offers only http/1.1 in ALPN (a TLS + EnableHTTP2 test reproduces the EOF failure without it), and closes its idle connections after each run. Since main gained a local-network guard after this PR was opened, the HTTP/1.1 client is now wrapped by it too. Your commit is kept with you as author; the review changes are a separate commit on top. Thanks!

@troke12
troke12 merged commit 9da07bc into troke12:main Oct 19, 2026
5 checks passed
@rigaz29
rigaz29 deleted the engine-http1-ranges branch October 19, 2026 05:01
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.

2 participants