Skip to content

Commit 91fc26f

Browse files
committed
refactor(p2p): dedupe forwarded Connection header and drop unused extractModel param
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
1 parent 8df0bb6 commit 91fc26f

4 files changed

Lines changed: 7 additions & 6 deletions

File tree

core/p2p/federated.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func servesModel(nd schema.NodeData, model string) bool {
130130
// cheapest source first: an explicit query value, then the JSON body "model"
131131
// field. Returns "" when it cannot be determined (for example a multipart or
132132
// websocket request), in which case the caller routes by load/affinity only.
133-
func extractModel(path, queryModel string, body []byte) string {
133+
func extractModel(queryModel string, body []byte) string {
134134
if strings.TrimSpace(queryModel) != "" {
135135
return queryModel
136136
}

core/p2p/federated_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (fs *FederatedServer) proxy(ctx context.Context, node *node.Node) error {
145145
// Websocket: no readable model; route by load only.
146146
workerID, _ = fs.selectPeer("", nil, now)
147147
default:
148-
model = extractModel(req.URL.Path, req.URL.Query().Get("model"), body)
148+
model = extractModel(req.URL.Query().Get("model"), body)
149149
workerID, chain = fs.selectPeer(model, body, now)
150150
}
151151

core/p2p/federated_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,20 @@ var _ = Describe("model-aware candidate building", func() {
102102
var _ = Describe("extractModel", func() {
103103
It("reads the JSON body model field", func() {
104104
body := []byte(`{"model":"llama-3","messages":[]}`)
105-
Expect(extractModel("/v1/chat/completions", "", body)).To(Equal("llama-3"))
105+
Expect(extractModel("", body)).To(Equal("llama-3"))
106106
})
107107

108108
It("prefers a path/query model over the body", func() {
109109
body := []byte(`{"model":"frombody"}`)
110-
Expect(extractModel("/x", "fromquery", body)).To(Equal("fromquery"))
110+
Expect(extractModel("fromquery", body)).To(Equal("fromquery"))
111111
})
112112

113113
It("returns empty when no model is present", func() {
114-
Expect(extractModel("/x", "", []byte(`{"messages":[]}`))).To(Equal(""))
114+
Expect(extractModel("", []byte(`{"messages":[]}`))).To(Equal(""))
115115
})
116116

117117
It("returns empty on non-JSON / unparseable body without panicking", func() {
118-
Expect(extractModel("/x", "", []byte("--multipart-boundary--"))).To(Equal(""))
118+
Expect(extractModel("", []byte("--multipart-boundary--"))).To(Equal(""))
119119
})
120120
})
121121

core/p2p/p2p.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func proxyHTTPToPeer(ctx context.Context, n *node.Node, serviceID string, conn n
152152
// io.Copy(conn, stream) blocks forever, leaking the goroutine, conn, and
153153
// stream. Websocket upgrades keep keep-alive: their duplex copy owns the
154154
// lifetime.
155+
req.Header.Del("Connection")
155156
req.Close = !duplex
156157
if err := req.Write(stream); err != nil {
157158
zlog.Error("Could not write request to peer", "error", err)

0 commit comments

Comments
 (0)