refactor(p2p): dedupe forwarded Connection header and drop unused extractModel param

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2026-06-01 22:07:10 +00:00
parent 8df0bb683b
commit 91fc26ff75
4 changed files with 7 additions and 6 deletions

View File

@@ -130,7 +130,7 @@ func servesModel(nd schema.NodeData, model string) bool {
// cheapest source first: an explicit query value, then the JSON body "model"
// field. Returns "" when it cannot be determined (for example a multipart or
// websocket request), in which case the caller routes by load/affinity only.
func extractModel(path, queryModel string, body []byte) string {
func extractModel(queryModel string, body []byte) string {
if strings.TrimSpace(queryModel) != "" {
return queryModel
}

View File

@@ -145,7 +145,7 @@ func (fs *FederatedServer) proxy(ctx context.Context, node *node.Node) error {
// Websocket: no readable model; route by load only.
workerID, _ = fs.selectPeer("", nil, now)
default:
model = extractModel(req.URL.Path, req.URL.Query().Get("model"), body)
model = extractModel(req.URL.Query().Get("model"), body)
workerID, chain = fs.selectPeer(model, body, now)
}

View File

@@ -102,20 +102,20 @@ var _ = Describe("model-aware candidate building", func() {
var _ = Describe("extractModel", func() {
It("reads the JSON body model field", func() {
body := []byte(`{"model":"llama-3","messages":[]}`)
Expect(extractModel("/v1/chat/completions", "", body)).To(Equal("llama-3"))
Expect(extractModel("", body)).To(Equal("llama-3"))
})
It("prefers a path/query model over the body", func() {
body := []byte(`{"model":"frombody"}`)
Expect(extractModel("/x", "fromquery", body)).To(Equal("fromquery"))
Expect(extractModel("fromquery", body)).To(Equal("fromquery"))
})
It("returns empty when no model is present", func() {
Expect(extractModel("/x", "", []byte(`{"messages":[]}`))).To(Equal(""))
Expect(extractModel("", []byte(`{"messages":[]}`))).To(Equal(""))
})
It("returns empty on non-JSON / unparseable body without panicking", func() {
Expect(extractModel("/x", "", []byte("--multipart-boundary--"))).To(Equal(""))
Expect(extractModel("", []byte("--multipart-boundary--"))).To(Equal(""))
})
})

View File

@@ -152,6 +152,7 @@ func proxyHTTPToPeer(ctx context.Context, n *node.Node, serviceID string, conn n
// io.Copy(conn, stream) blocks forever, leaking the goroutine, conn, and
// stream. Websocket upgrades keep keep-alive: their duplex copy owns the
// lifetime.
req.Header.Del("Connection")
req.Close = !duplex
if err := req.Write(stream); err != nil {
zlog.Error("Could not write request to peer", "error", err)