@@ -23,6 +23,23 @@ import (
2323
2424type contextKey bool
2525
26+ // readRequestBody reads up to maxSize bytes from the request body and writes
27+ // an appropriate HTTP error if reading fails. Returns (body, true) on success
28+ // or (nil, false) after writing the error response.
29+ func readRequestBody (w http.ResponseWriter , r * http.Request , maxSize int64 ) ([]byte , bool ) {
30+ body , err := io .ReadAll (http .MaxBytesReader (w , r .Body , maxSize ))
31+ if err != nil {
32+ var maxBytesError * http.MaxBytesError
33+ if errors .As (err , & maxBytesError ) {
34+ http .Error (w , "request too large" , http .StatusBadRequest )
35+ } else {
36+ http .Error (w , "failed to read request body" , http .StatusInternalServerError )
37+ }
38+ return nil , false
39+ }
40+ return body , true
41+ }
42+
2643const preloadOnlyKey contextKey = false
2744
2845// HTTPHandler handles HTTP requests for the scheduler.
@@ -132,14 +149,8 @@ func (h *HTTPHandler) handleOpenAIInference(w http.ResponseWriter, r *http.Reque
132149
133150 // Read the entire request body. We put some basic size constraints in place
134151 // to avoid DoS attacks. We do this early to avoid client write timeouts.
135- body , err := io .ReadAll (http .MaxBytesReader (w , r .Body , maximumOpenAIInferenceRequestSize ))
136- if err != nil {
137- var maxBytesError * http.MaxBytesError
138- if errors .As (err , & maxBytesError ) {
139- http .Error (w , "request too large" , http .StatusBadRequest )
140- } else {
141- http .Error (w , "failed to read request body" , http .StatusInternalServerError )
142- }
152+ body , ok := readRequestBody (w , r , maximumOpenAIInferenceRequestSize )
153+ if ! ok {
143154 return
144155 }
145156
@@ -338,14 +349,8 @@ func (h *HTTPHandler) GetDiskUsage(w http.ResponseWriter, _ *http.Request) {
338349// Unload unloads the specified runners (backend, model) from the backend.
339350// Currently, this doesn't work for runners that are handling an OpenAI request.
340351func (h * HTTPHandler ) Unload (w http.ResponseWriter , r * http.Request ) {
341- body , err := io .ReadAll (http .MaxBytesReader (w , r .Body , maximumOpenAIInferenceRequestSize ))
342- if err != nil {
343- var maxBytesError * http.MaxBytesError
344- if errors .As (err , & maxBytesError ) {
345- http .Error (w , "request too large" , http .StatusBadRequest )
346- } else {
347- http .Error (w , "failed to read request body" , http .StatusInternalServerError )
348- }
352+ body , ok := readRequestBody (w , r , maximumOpenAIInferenceRequestSize )
353+ if ! ok {
349354 return
350355 }
351356
@@ -371,14 +376,8 @@ type installBackendRequest struct {
371376// InstallBackend handles POST <inference-prefix>/install-backend requests.
372377// It triggers on-demand installation of a deferred backend.
373378func (h * HTTPHandler ) InstallBackend (w http.ResponseWriter , r * http.Request ) {
374- body , err := io .ReadAll (http .MaxBytesReader (w , r .Body , maximumOpenAIInferenceRequestSize ))
375- if err != nil {
376- var maxBytesError * http.MaxBytesError
377- if errors .As (err , & maxBytesError ) {
378- http .Error (w , "request too large" , http .StatusBadRequest )
379- } else {
380- http .Error (w , "failed to read request body" , http .StatusInternalServerError )
381- }
379+ body , ok := readRequestBody (w , r , maximumOpenAIInferenceRequestSize )
380+ if ! ok {
382381 return
383382 }
384383
@@ -414,14 +413,8 @@ func (h *HTTPHandler) Configure(w http.ResponseWriter, r *http.Request) {
414413 return
415414 }
416415
417- body , err := io .ReadAll (http .MaxBytesReader (w , r .Body , maximumOpenAIInferenceRequestSize ))
418- if err != nil {
419- var maxBytesError * http.MaxBytesError
420- if errors .As (err , & maxBytesError ) {
421- http .Error (w , "request too large" , http .StatusBadRequest )
422- } else {
423- http .Error (w , "failed to read request body" , http .StatusInternalServerError )
424- }
416+ body , ok := readRequestBody (w , r , maximumOpenAIInferenceRequestSize )
417+ if ! ok {
425418 return
426419 }
427420
@@ -433,7 +426,7 @@ func (h *HTTPHandler) Configure(w http.ResponseWriter, r *http.Request) {
433426 return
434427 }
435428
436- backend , err = h .scheduler .ConfigureRunner (r .Context (), backend , configureRequest , r .UserAgent ())
429+ backend , err : = h .scheduler .ConfigureRunner (r .Context (), backend , configureRequest , r .UserAgent ())
437430 if err != nil {
438431 if errors .Is (err , errRunnerAlreadyActive ) {
439432 http .Error (w , err .Error (), http .StatusConflict )
0 commit comments