carlos / platform Public

tunnel: keep x/net's HTTP/2 client and server, and say why

golang.org/x/net/http2's ClientConn, Transport and Server are
deprecated in favour of net/http, but net/http cannot do what the
tunnel needs today, so each site carries a lint:ignore naming the gap:

- http.ClientConn has no Ping, which pingLoop's liveness check uses,
  and no Closing state, which tunnelConnLost reads.
- http.Transport.NewClientConn dials an address; the hub has to adopt
  a connection it already hijacked.
- http.Server has no ServeConn. serveConn's drain and refusingConn are
  built on how x/net's server sends GOAWAY and resets a stream, so
  moving changes request-path behaviour.

Tests that stand in for the hub or the box use the same types. No
behaviour changes here: comments only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 51914bac50e3ea95a3afada4f4b9944b224d5cea parent 433776e
11 files changed, +44 −6
  • internal/activator/tunnel_refuse_test.go +5 −2
  • internal/edge/replica_reach_test.go +1 −0
  • internal/edge/replicaproxy_test.go +1 −0
  • internal/edge/tunnel.go +7 −2
  • internal/edge/upgrades_test.go +3 −0
  • internal/tunnelclient/drain_test.go +3 −0
  • internal/tunnelclient/refuse_test.go +6 −0
  • internal/tunnelclient/tlsstate_test.go +3 −0
  • internal/tunnelclient/tunnelclient_test.go +1 −0
  • internal/tunnelclient/upgrade.go +11 −2
  • internal/tunnelclient/upgrade_box_test.go +3 −0
diff --git a/internal/activator/tunnel_refuse_test.go b/internal/activator/tunnel_refuse_test.go
index 1f10e8c4c..2222a3dcd 100644
--- a/internal/activator/tunnel_refuse_test.go
+++ b/internal/activator/tunnel_refuse_test.go
@@ -34,8 +34,9 @@ func (b *stoppedOutsideBackend) StoppedOutside(string) bool { return b.stopped.L
// streams arriving from a fake hub's ClientConn (cc). Its instance waits in
// awaitStartup (a.Startup open) until the test stops the activator.
type refuseBox struct {
- a *Service
- b *stoppedOutsideBackend
+ a *Service
+ b *stoppedOutsideBackend
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc *http2.ClientConn
drain chan struct{}
entered chan struct{}
@@ -59,6 +60,7 @@ func newRefuseBox(t *testing.T, rt registry.Route) *refuseBox {
}
x.a = New(Config{Bucket: "test-bucket", Idle: time.Hour}, x.b, reg)
x.a.Startup = make(chan struct{})
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
conns := make(chan *http2.ClientConn, 1)
hub := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, bufrw, err := w.(http.Hijacker).Hijack()
@@ -68,6 +70,7 @@ func newRefuseBox(t *testing.T, rt registry.Route) *refuseBox {
}
fmt.Fprintf(bufrw, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: carlos-tunnel/1\r\nConnection: Upgrade\r\n\r\n")
bufrw.Flush()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(conn)
if err != nil {
t.Errorf("h2 client: %v", err)
diff --git a/internal/edge/replica_reach_test.go b/internal/edge/replica_reach_test.go
index 7bb01688b..18f4dab7f 100644
--- a/internal/edge/replica_reach_test.go
+++ b/internal/edge/replica_reach_test.go
@@ -122,6 +122,7 @@ func replicaFramerBox(t *testing.T, hub *TunnelHub, p *replicaProber, host, box,
}
}()
wire := &tunnelWire{Conn: a, closed: make(chan struct{})}
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(wire)
if err != nil {
t.Fatal(err)
diff --git a/internal/edge/replicaproxy_test.go b/internal/edge/replicaproxy_test.go
index aff35006d..93f42ae18 100644
--- a/internal/edge/replicaproxy_test.go
+++ b/internal/edge/replicaproxy_test.go
@@ -35,6 +35,7 @@ func replicaProxyFixture(t *testing.T, handlers ...http.HandlerFunc) (*TunnelHub
tunnelclient.ServeWith(b, tunnelclient.Mark(handler), tunnelclient.DefaultLimits)
}()
wire := &tunnelWire{Conn: a, closed: make(chan struct{})}
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(wire)
if err != nil {
t.Fatal(err)
diff --git a/internal/edge/tunnel.go b/internal/edge/tunnel.go
index fb8f97050..d43d18d5d 100644
--- a/internal/edge/tunnel.go
+++ b/internal/edge/tunnel.go
@@ -98,8 +98,9 @@ type tunnelConn struct {
generation uint64
probeBoot string // first valid platform answer on this connection; guarded by hub.mu
closed <-chan struct{}
- cc *http2.ClientConn
- raw net.Conn
+ //lint:ignore SA1019 net/http's ClientConn has no Ping or Closing state, which pingLoop and tunnelConnLost need
+ cc *http2.ClientConn
+ raw net.Conn
// dynamic records that this conn authenticated against the registry-backed
// (fleet) credential source rather than the static env map — the one fact
// ServeHTTP knows and pingLoop needs for pingCadence.
@@ -281,7 +282,9 @@ func (h *TunnelHub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
wire := &tunnelWire{Conn: conn, closed: make(chan struct{})}
conn = wire
+ //lint:ignore SA1019 net/http's Transport.NewClientConn dials an address and cannot adopt this hijacked conn
tr := &http2.Transport{}
+ //lint:ignore SA1019 net/http's Transport.NewClientConn dials an address and cannot adopt this hijacked conn
cc, err := tr.NewClientConn(conn)
if err != nil {
log.Printf("edge: tunnel %s: h2 client conn: %v", name, err)
@@ -392,6 +395,8 @@ func (h *TunnelHub) recordLinkSeen(name, linkID string, tc *tunnelConn, connecte
// dynamic carries the credential source through from ServeHTTP: fleet
// channels reap on a tighter bound because that bound is their failover
// window (see pingCadence).
+//
+//lint:ignore SA1019 net/http's ClientConn has no Ping
func (h *TunnelHub) pingLoop(name string, cc *http2.ClientConn, raw net.Conn, dynamic bool) {
every, timeout := pingCadence(dynamic)
for {
diff --git a/internal/edge/upgrades_test.go b/internal/edge/upgrades_test.go
index cbf405454..9e9214b62 100644
--- a/internal/edge/upgrades_test.go
+++ b/internal/edge/upgrades_test.go
@@ -290,11 +290,14 @@ func (c *stuckConn) Read(p []byte) (int, error) {
// stuckPair serves handler (through Mark, as Loop does) on a tunnel whose
// box end can be made to stop reading.
+//
+//lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
func stuckPair(t *testing.T, handler http.Handler) (*http2.ClientConn, *stuckConn) {
t.Helper()
boxEnd, hubEnd := net.Pipe()
sc := &stuckConn{Conn: boxEnd, stuck: make(chan struct{}), absorbed: make(chan struct{}), unstick: make(chan struct{})}
go tunnelclient.ServeWith(sc, tunnelclient.Mark(handler), tunnelclient.DefaultLimits)
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(hubEnd)
if err != nil {
t.Fatal(err)
diff --git a/internal/tunnelclient/drain_test.go b/internal/tunnelclient/drain_test.go
index 6976c8824..015af0402 100644
--- a/internal/tunnelclient/drain_test.go
+++ b/internal/tunnelclient/drain_test.go
@@ -20,6 +20,7 @@ import (
// dialing again. A drain closed before Loop starts dials nothing.
func TestLoopDrain(t *testing.T) {
var registrations atomic.Int32
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
conns := make(chan *http2.ClientConn, 4)
edge := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
registrations.Add(1)
@@ -30,6 +31,7 @@ func TestLoopDrain(t *testing.T) {
}
fmt.Fprintf(bufrw, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: carlos-tunnel/1\r\nConnection: Upgrade\r\n\r\n")
bufrw.Flush()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(conn)
if err != nil {
t.Errorf("h2 client: %v", err)
@@ -56,6 +58,7 @@ func TestLoopDrain(t *testing.T) {
defer close(done)
Loop(context.Background(), Config{URL: edge.URL, Name: "pi/box-1", Token: "t", Handler: handler, Drain: drain, Logf: t.Logf})
}()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
var cc *http2.ClientConn
select {
case cc = <-conns:
diff --git a/internal/tunnelclient/refuse_test.go b/internal/tunnelclient/refuse_test.go
index 027c2e9f0..8cf5b2ce0 100644
--- a/internal/tunnelclient/refuse_test.go
+++ b/internal/tunnelclient/refuse_test.go
@@ -17,11 +17,14 @@ import (
// refuseRig is a box h2 server (serveConn with a drain) and the hub's
// ClientConn on the other end of a pipe.
+//
+//lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
func refuseRig(t *testing.T, handler http.Handler, drain <-chan struct{}) *http2.ClientConn {
t.Helper()
a, b := net.Pipe()
done := make(chan struct{})
go func() { defer close(done); serveConn(b, handler, DefaultLimits, drain) }()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(a)
if err != nil {
t.Fatal(err)
@@ -30,6 +33,7 @@ func refuseRig(t *testing.T, handler http.Handler, drain <-chan struct{}) *http2
return cc
}
+//lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
func refuseDo(t *testing.T, cc *http2.ClientConn, method, body string) (*http.Response, error) {
t.Helper()
var rd io.Reader
@@ -131,8 +135,10 @@ func TestStreamGateRefusesOnceDraining(t *testing.T) {
done := make(chan struct{})
go func() {
defer close(done)
+ //lint:ignore SA1019 serves the tunnel with the x/net server serveConn uses in production
(&http2.Server{}).ServeConn(rc, &http2.ServeConnOpts{Handler: streamGate{h: http.HandlerFunc(func(http.ResponseWriter, *http.Request) { calls.Add(1) }), drain: drain, conn: rc}})
}()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(a)
if err != nil {
t.Fatal(err)
diff --git a/internal/tunnelclient/tlsstate_test.go b/internal/tunnelclient/tlsstate_test.go
index 0f4963f30..c7f3d94d3 100644
--- a/internal/tunnelclient/tlsstate_test.go
+++ b/internal/tunnelclient/tlsstate_test.go
@@ -57,6 +57,7 @@ func TestFleetTunnelKeepsHTTPS(t *testing.T) {
edge.ServeHTTP(w, r)
})
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
conns := make(chan *http2.ClientConn, 1)
hub := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, bufrw, err := w.(http.Hijacker).Hijack()
@@ -66,6 +67,7 @@ func TestFleetTunnelKeepsHTTPS(t *testing.T) {
}
fmt.Fprintf(bufrw, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: carlos-tunnel/1\r\nConnection: Upgrade\r\n\r\n")
bufrw.Flush()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(conn)
if err != nil {
t.Errorf("h2 client: %v", err)
@@ -90,6 +92,7 @@ func TestFleetTunnelKeepsHTTPS(t *testing.T) {
Loop(ctx, Config{URL: hub.URL, Name: "pi/box-1", Token: "t", Handler: handler, Drain: drain, Logf: t.Logf})
}()
defer func() { cancel(); <-done }()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
var cc *http2.ClientConn
select {
case cc = <-conns:
diff --git a/internal/tunnelclient/tunnelclient_test.go b/internal/tunnelclient/tunnelclient_test.go
index 64e448cb5..d8488eab1 100644
--- a/internal/tunnelclient/tunnelclient_test.go
+++ b/internal/tunnelclient/tunnelclient_test.go
@@ -30,6 +30,7 @@ func fakeTunnelEdge(t *testing.T, wantToken, wantName string) (*httptest.Server,
}
fmt.Fprintf(bufrw, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: carlos-tunnel/1\r\nConnection: Upgrade\r\n\r\n")
bufrw.Flush()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(conn)
if err != nil {
t.Errorf("h2 client: %v", err)
diff --git a/internal/tunnelclient/upgrade.go b/internal/tunnelclient/upgrade.go
index d5630e8ed..774e132bf 100644
--- a/internal/tunnelclient/upgrade.go
+++ b/internal/tunnelclient/upgrade.go
@@ -114,12 +114,17 @@ func ServeWith(conn net.Conn, handler http.Handler, l Limits) {
// connection only once they are done (goAwayTimeout after the last), or
// the process exits under them, as before.
func serveConn(conn net.Conn, handler http.Handler, l Limits, drain <-chan struct{}) {
+ //lint:ignore SA1019 moving to http.Server.Serve changes how drain's GOAWAY and stream refusal reach this conn
srv := &http2.Server{
- MaxConcurrentStreams: l.MaxStreams,
- MaxUploadBufferPerStream: l.StreamWindow,
+ //lint:ignore SA1019 moving to http.Server.Serve changes how drain's GOAWAY and stream refusal reach this conn
+ MaxConcurrentStreams: l.MaxStreams,
+ //lint:ignore SA1019 moving to http.Server.Serve changes how drain's GOAWAY and stream refusal reach this conn
+ MaxUploadBufferPerStream: l.StreamWindow,
+ //lint:ignore SA1019 moving to http.Server.Serve changes how drain's GOAWAY and stream refusal reach this conn
MaxUploadBufferPerConnection: l.ConnWindow,
}
if drain == nil {
+ //lint:ignore SA1019 moving to http.Server.Serve changes how drain's GOAWAY and stream refusal reach this conn
srv.ServeConn(conn, &http2.ServeConnOpts{Handler: handler})
return
}
@@ -130,6 +135,7 @@ func serveConn(conn net.Conn, handler http.Handler, l Limits, drain <-chan struc
registered := make(chan struct{})
var once sync.Once
base := &http.Server{ConnState: func(net.Conn, http.ConnState) { once.Do(func() { close(registered) }) }}
+ //lint:ignore SA1019 moving to http.Server.Serve changes how drain's GOAWAY and stream refusal reach this conn
if err := http2.ConfigureServer(base, srv); err != nil {
conn.Close()
return
@@ -152,6 +158,7 @@ func serveConn(conn net.Conn, handler http.Handler, l Limits, drain <-chan struc
_ = base.Shutdown(context.Background())
}()
rc := newRefusingConn(conn)
+ //lint:ignore SA1019 moving to http.Server.Serve changes how drain's GOAWAY and stream refusal reach this conn
srv.ServeConn(servedConn(rc), &http2.ServeConnOpts{Handler: streamGate{h: handler, drain: drain, conn: rc}, BaseConfig: base})
}
@@ -477,6 +484,8 @@ var hookHandshake func()
// or after the 101. x/net alone does not watch the context while it waits
// on an open request body, so the context's end fails the pipe, which
// x/net answers with RST_STREAM.
+//
+//lint:ignore SA1019 the hub's tunnel connection is x/net's ClientConn (internal/edge/tunnel.go)
func RoundTripUpgrade(ctx context.Context, cc *http2.ClientConn, req *http.Request) (*http.Response, error) {
tok := req.Header.Get("Upgrade")
if !ValidUpgradeToken(tok) {
diff --git a/internal/tunnelclient/upgrade_box_test.go b/internal/tunnelclient/upgrade_box_test.go
index f276f909d..5f1937dbe 100644
--- a/internal/tunnelclient/upgrade_box_test.go
+++ b/internal/tunnelclient/upgrade_box_test.go
@@ -20,6 +20,8 @@ import (
// h2Pair serves handler with ServeWith on one end of an in-memory pipe and
// returns an HTTP/2 client conn on the other: the box half of the tunnel
// with nothing between the test and Mark but x/net.
+//
+//lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
func h2Pair(t *testing.T, handler http.Handler) *http2.ClientConn {
t.Helper()
box, hub := net.Pipe()
@@ -28,6 +30,7 @@ func h2Pair(t *testing.T, handler http.Handler) *http2.ClientConn {
defer close(done)
ServeWith(box, handler, DefaultLimits)
}()
+ //lint:ignore SA1019 drives the x/net ClientConn the hub uses in production (internal/edge/tunnel.go)
cc, err := (&http2.Transport{}).NewClientConn(hub)
if err != nil {
t.Fatalf("h2 client: %v", err)