carlos / platform Public

edge: file a relayed websocket upgrade as 101, not 200 (#511 H2)

Drill 3's hub minute counts had 41 more 2xx than the client. 5 of them
were the drill's websocket dials: httputil.ReverseProxy writes an
upstream's 101 to the hijacked connection, never through WriteHeader, so
the counting writer's status stayed 0 and observeRecords filed the dial,
when its socket ended, as "nothing written: 200".

The counting writer now notes a successful Hijack, and observeRecords
files such a response as the 101 it was. A minute count puts a 101 in no
status class (m_get still counts it). Analytics and the burst limit read
the writer as before.

TestUpgradeRecordedAs101 relays a websocket through ServeHTTP and checks
the record and the minute count.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 214da4fc6f0647dc0475d926de0761f07f5e0696 parent 8a629cd
3 files changed, +115 −2
  • internal/edge/reqrecords.go +9 −1
  • internal/edge/reqrecords_upgrade_test.go +95 −0
  • internal/edge/usagemeter.go +11 −1
diff --git a/internal/edge/reqrecords.go b/internal/edge/reqrecords.go
index 329897d98..e12385e2d 100644
--- a/internal/edge/reqrecords.go
+++ b/internal/edge/reqrecords.go
@@ -71,7 +71,15 @@ func (e *Edge) observeRecords(rt registry.Route, viaTunnel bool, r *http.Request
return
}
status := ob.status
- if status == 0 {
+ switch {
+ case status == 0 && ob.hijacked:
+ // An upgrade the proxy relayed: the client got the upstream's 101
+ // on the hijacked connection, filed when the socket ends. It is
+ // no 2xx, so the minute counts leave it out of every class (#511
+ // drill 3, H2: each websocket dial was one 2xx the client never
+ // saw).
+ status = http.StatusSwitchingProtocols
+ case status == 0:
status = http.StatusOK // nothing written: net/http answers 200
}
x := requestResponse(rt, r, status)
diff --git a/internal/edge/reqrecords_upgrade_test.go b/internal/edge/reqrecords_upgrade_test.go
new file mode 100644
index 000000000..b45909821
--- /dev/null
+++ b/internal/edge/reqrecords_upgrade_test.go
@@ -0,0 +1,95 @@
+package edge
+
+import (
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/reqrec"
+)
+
+// chanRecorder hands each filed Response to the test as it is filed.
+type chanRecorder struct{ resp chan reqrec.Response }
+
+func (c chanRecorder) Response(x reqrec.Response) { c.resp <- x }
+func (chanRecorder) Match(reqrec.Match) {}
+func (chanRecorder) Trip(reqrec.Trip) {}
+
+// minuteSink keeps the minute count records a reqrec.Recorder files.
+type minuteSink struct {
+ mu sync.Mutex
+ recs []map[string]string
+}
+
+func (s *minuteSink) Edge(string, string, string, string, string, map[string]string) {}
+func (s *minuteSink) EdgeSummary(_, _, _, _, _ string, f map[string]string) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ s.recs = append(s.recs, f)
+}
+
+// TestUpgradeRecordedAs101 is drill 3's H2 hub half: the hub filed every
+// websocket dial it relayed as a 200, because the 101 goes out on the
+// hijacked connection and the counting writer never saw a status. Its
+// minute counts then had one 2xx per dial that the client (rightly) did
+// not count as one: 5 of the 41 extra 2xx. A relayed upgrade is filed as
+// the 101 it was, and a minute count leaves it out of every status class.
+func TestUpgradeRecordedAs101(t *testing.T) {
+ e := edgeWithBackend(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ conn, rw, err := w.(http.Hijacker).Hijack()
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ defer conn.Close()
+ io.WriteString(rw, "HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n")
+ rw.Flush()
+ io.Copy(conn, rw)
+ }))
+ rec := chanRecorder{resp: make(chan reqrec.Response, 4)}
+ e.EnableRequestRecords(rec)
+ front := httptest.NewServer(e)
+ defer front.Close()
+ c, br, resp, body, err := rawUpgrade(front.URL, "app.test")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if resp.StatusCode != http.StatusSwitchingProtocols {
+ c.Close()
+ t.Fatalf("upgrade: %d %q", resp.StatusCode, body)
+ }
+ io.WriteString(c, "ping\n")
+ if line, err := br.ReadString('\n'); err != nil || line != "ping\n" {
+ c.Close()
+ t.Fatalf("socket: %q %v", line, err)
+ }
+ c.Close()
+ var x reqrec.Response
+ select {
+ case x = <-rec.resp:
+ case <-time.After(5 * time.Second):
+ t.Fatal("the upgrade was never filed")
+ }
+ // TestUpgradeRecordedAs101
+ if x.Status != http.StatusSwitchingProtocols || x.Method != "GET" {
+ t.Fatalf("filed as %d %s, want 101 GET", x.Status, x.Method)
+ }
+ now := time.Date(2026, 11, 12, 13, 13, 30, 0, time.UTC)
+ sink := &minuteSink{}
+ r := reqrec.NewRecorder(sink, nil, func() time.Time { return now })
+ x.When = now
+ r.Response(x)
+ r.Flush(now.Add(time.Minute))
+ var minute map[string]string
+ for _, f := range sink.recs {
+ if f["kind"] == "minute" {
+ minute = f
+ }
+ }
+ if minute == nil || minute["s2xx"] != "0" || minute["s3xx"] != "0" || minute["s4xx"] != "0" || minute["s5xx"] != "0" || minute["m_get"] != "1" {
+ t.Fatalf("minute counts for one websocket dial: %v", minute)
+ }
+}
diff --git a/internal/edge/usagemeter.go b/internal/edge/usagemeter.go
index fced0b8fb..496c4be27 100644
--- a/internal/edge/usagemeter.go
+++ b/internal/edge/usagemeter.go
@@ -152,6 +152,12 @@ type countingWriter struct {
status int // final status (>= 200); 0 until one is written
ctype string // Content-Type at the final WriteHeader
wroteHdr bool
+ // hijacked: a Hijack succeeded. On the serving path only
+ // httputil.ReverseProxy hijacks, to relay an upstream's 101, which it
+ // writes to the hijacked connection, never through WriteHeader, so
+ // status stays 0. The request record reads this to file the answer as
+ // the 101 it was (observeRecords).
+ hijacked bool
}
// WriteHeader latches the first final status. 1xx are forwarded and never
@@ -188,7 +194,11 @@ func (c *countingWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if !ok {
return nil, nil, fmt.Errorf("edge: underlying ResponseWriter does not support http.Hijacker")
}
- return hj.Hijack()
+ conn, brw, err := hj.Hijack()
+ if err == nil {
+ c.hijacked = true
+ }
+ return conn, brw, err
}
// Unwrap lets http.NewResponseController reach the underlying writer (write