A route may have its own listener: every request there is that route, paths unprefixed

Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
This commit is contained in:
2026-09-25 19:59:21 -07:00
parent 35b07ece81
commit fa1c398fc4
10 changed files with 392 additions and 12 deletions
+112
View File
@@ -0,0 +1,112 @@
package proxy_test
// v2.3 task 03: Handler.ForRoute serves one route with unprefixed paths, for a route's dedicated
// listener.
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.wntrmute.dev/kyle/crossbar/internal/proxy"
)
// dedicated serves r's route on its own test server, sharing r's health, leases, limiter and
// store, as main does for a route with listen set.
func dedicated(t *testing.T, r *rig, route string) *httptest.Server {
p := proxy.New(r.cfg, r.health, r.leases, r.lim, r.store, nil)
srv := httptest.NewServer(p.ForRoute(route))
t.Cleanup(srv.Close)
return srv
}
func call(t *testing.T, method, url, body string, hdr ...string) (*http.Response, string) {
t.Helper()
var req *http.Request
if body != "" {
req, _ = http.NewRequest(method, url, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
} else {
req, _ = http.NewRequest(method, url, nil)
}
for i := 0; i+1 < len(hdr); i += 2 {
req.Header.Set(hdr[i], hdr[i+1])
}
resp, err := controlClient.Do(req)
if err != nil {
t.Fatalf("%s %s: %v", method, url, err)
}
return resp, drain(resp)
}
func TestForRouteServesUnprefixedPaths(t *testing.T) {
alpha, beta := newUpstream(t, "alpha"), newUpstream(t, "beta")
r := newRig(t, affinityHosts, alpha, beta)
srv := dedicated(t, r, "bm")
resp, body := call(t, http.MethodPost, srv.URL+"/v1/chat/completions", conversation(1, 1))
if resp.StatusCode != 200 {
t.Fatalf("chat on the dedicated listener: %d %s", resp.StatusCode, body)
}
host := resp.Header.Get("X-Crossbar-Host")
up := map[string]*upstream{"alpha": alpha, "beta": beta}[host]
if up == nil || up.lastReq().path != "/v1/chat/completions" {
t.Fatalf("upstream %q saw %+v, want /v1/chat/completions unchanged", host, up.lastReq())
}
resp, _ = call(t, http.MethodGet, srv.URL+"/slots?model=shared", "")
if resp.StatusCode != 200 || resp.Header.Get("X-Crossbar-Host") != host || up.lastReq().path != "/slots?model=shared" {
t.Errorf("/slots: %d on %q (last %+v), want 200 on %q", resp.StatusCode, resp.Header.Get("X-Crossbar-Host"), up.lastReq(), host)
}
resp, _ = call(t, http.MethodPost, srv.URL+"/v1/chat/completions/control", `{"id":"chatcmpl-1","action":"reasoning_end","model":"shared"}`)
if resp.StatusCode != 200 || resp.Header.Get("X-Crossbar-Host") != host {
t.Errorf("/control: %d on %q, want 200 on %q", resp.StatusCode, resp.Header.Get("X-Crossbar-Host"), host)
}
// The chat is accounted to the route the listener serves.
waitUntil(t, func() bool { return r.rows("bm") == 1 })
// The same route through the main listener shares the lease: same host.
resp = r.do(http.MethodPost, "/bm/v1/chat/completions", conversation(2, 1))
drain(resp)
if resp.Header.Get("X-Crossbar-Host") != host {
t.Errorf("main listener /bm went to %q, dedicated to %q; one route, one lease", resp.Header.Get("X-Crossbar-Host"), host)
}
}
func TestForRouteRefusals(t *testing.T) {
alpha, beta := newUpstream(t, "alpha"), newUpstream(t, "beta")
r := newRig(t, affinityHosts, alpha, beta)
srv := dedicated(t, r, "bm")
for _, tc := range []struct {
name, method, path string
hdr []string
want int
msg string
}{
{"a prefixed path is not stripped", http.MethodGet, "/bm/v1/models", nil, 404, "not found"},
{"no admin here", http.MethodGet, "/_crossbar/hosts", nil, 404, "not found"},
{"root", http.MethodGet, "/", nil, 404, "not found"},
{"header naming another route", http.MethodGet, "/v1/models", []string{"X-Crossbar-Route", "r"}, 400, "conflicting route"},
} {
resp, body := call(t, tc.method, srv.URL+tc.path, "", tc.hdr...)
var e map[string]string
if resp.StatusCode != tc.want || json.Unmarshal([]byte(body), &e) != nil || e["error"] != tc.msg {
t.Errorf("%s: %d %s, want %d %q", tc.name, resp.StatusCode, body, tc.want, tc.msg)
}
}
// A header naming this same route is harmless.
resp, body := call(t, http.MethodGet, srv.URL+"/v1/models", "", "X-Crossbar-Route", "bm")
if resp.StatusCode != 200 {
t.Errorf("header naming the listener's own route: %d %s, want 200", resp.StatusCode, body)
}
}
func TestForRouteUnknownRoute(t *testing.T) {
alpha, beta := newUpstream(t, "alpha"), newUpstream(t, "beta")
r := newRig(t, affinityHosts, alpha, beta)
srv := dedicated(t, r, "nope")
resp, body := call(t, http.MethodGet, srv.URL+"/v1/models", "")
if resp.StatusCode != 404 || !strings.Contains(body, "unknown route") {
t.Errorf("ForRoute(unknown): %d %s, want 404 unknown route", resp.StatusCode, body)
}
}
+33 -3
View File
@@ -198,15 +198,45 @@ func peekModel(r *http.Request) (string, []byte, error) {
return req.Model, body, nil
}
// ServeHTTP routes, fingerprints, leases a host, queues per (host, model), forwards with streaming,
// tees the response for usage/timings, and records one accounting row. Every error answer is JSON
// {"error":"…"}.
// ServeHTTP routes, then serves the request over the shared flow below: fingerprint, lease a host,
// queue per (host, model), forward with streaming, tee the response for usage/timings, and record
// one accounting row. Every error answer is JSON {"error":"…"}.
func (p *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
route, rest, code, msg := p.route(r)
if code != 0 {
p.writeError(w, code, msg)
return
}
p.serve(w, r, route, rest)
}
// ForRoute serves route alone, for its dedicated listener: every request there is this route, the
// whole path passed upstream as it is (no route segment is taken from it). A request from an unknown
// route is 404; a different X-Crossbar-Route header is a 400 (an equal one is ignored); a prefixed
// path or a path outside the allowed set is 404. Then it runs the same serve flow as ServeHTTP.
func (p *Handler) ForRoute(route string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, _, ok := p.cfg.Route(route); !ok {
p.writeError(w, http.StatusNotFound, "unknown route")
return
}
if hdr := r.Header.Get(RouteHeader); hdr != "" && hdr != route {
p.writeError(w, http.StatusBadRequest, "conflicting route")
return
}
rest := r.URL.Path
if !allowedPath(rest) {
p.writeError(w, http.StatusNotFound, "not found")
return
}
p.serve(w, r, route, rest)
})
}
// serve fingerprints, leases a host, queues per (host, model), forwards with streaming, tees the
// response for usage/timings, and records one accounting row. Both ServeHTTP and ForRoute reach this
// after they have settled the route and the upstream path (rest).
func (p *Handler) serve(w http.ResponseWriter, r *http.Request, route, rest string) {
routeCfg, _, _ := p.cfg.Route(route)
isControl := isControlCall(r.Method, rest)