@@ -657,61 +657,61 @@ func (c *Client) ConfigureBackend(request scheduling.ConfigureRequest) error {
657657 return nil
658658}
659659
660- func (c * Client ) RequestsStream (modelFilter string , includeExisting bool ) (io.ReadCloser , error ) {
661- path := c .modelRunner .URL (inference .InferencePrefix + "/requests/stream" )
660+ // Requests returns a response body and a cancel function to ensure proper cleanup.
661+ func (c * Client ) Requests (modelFilter string , streaming bool , includeExisting bool ) (io.ReadCloser , func (), error ) {
662+ path := c .modelRunner .URL (inference .InferencePrefix + "/requests" )
662663 var queryParams []string
663664 if modelFilter != "" {
664665 queryParams = append (queryParams , "model=" + modelFilter )
665666 }
666- if includeExisting {
667+ if includeExisting && streaming {
667668 queryParams = append (queryParams , "include_existing=true" )
668669 }
669670 if len (queryParams ) > 0 {
670671 path += "?" + strings .Join (queryParams , "&" )
671672 }
673+
672674 req , err := http .NewRequest (http .MethodGet , path , nil )
673675 if err != nil {
674- return nil , fmt .Errorf ("failed to create request: %w" , err )
675- }
676- req .Header .Set ("Accept" , "text/event-stream" )
677- req .Header .Set ("Cache-Control" , "no-cache" )
678- req .Header .Set ("User-Agent" , "docker-model-cli/" + Version )
679- resp , err := c .modelRunner .Client ().Do (req )
680- if err != nil {
681- return nil , fmt .Errorf ("failed to connect to stream: %w" , err )
682- }
683- if resp .StatusCode != http .StatusOK {
684- resp .Body .Close ()
685- return nil , fmt .Errorf ("stream request failed with status: %d" , resp .StatusCode )
676+ return nil , nil , fmt .Errorf ("failed to create request: %w" , err )
686677 }
687- return resp .Body , nil
688- }
689678
690- func (c * Client ) RequestsList (modelFilter string ) (string , error ) {
691- path := inference .InferencePrefix + "/requests"
692- if modelFilter != "" {
693- path += "?model=" + modelFilter
679+ if streaming {
680+ req .Header .Set ("Accept" , "text/event-stream" )
681+ req .Header .Set ("Cache-Control" , "no-cache" )
682+ } else {
683+ req .Header .Set ("Accept" , "application/json" )
694684 }
695- resp , err := c .doRequest (http .MethodGet , path , nil )
685+ req .Header .Set ("User-Agent" , "docker-model-cli/" + Version )
686+
687+ resp , err := c .modelRunner .Client ().Do (req )
696688 if err != nil {
697- return "" , c .handleQueryError (err , path )
689+ if streaming {
690+ return nil , nil , c .handleQueryError (fmt .Errorf ("failed to connect to stream: %w" , err ), path )
691+ }
692+ return nil , nil , c .handleQueryError (err , path )
698693 }
699- defer resp .Body .Close ()
700694
701- if resp .StatusCode != http .StatusOK && resp .StatusCode != http .StatusNotFound {
702- return "" , fmt .Errorf ("failed to list requests: %s" , resp .Status )
703- }
695+ if resp .StatusCode != http .StatusOK {
696+ if resp .StatusCode == http .StatusNotFound {
697+ body , _ := io .ReadAll (resp .Body )
698+ resp .Body .Close ()
699+ return nil , nil , fmt .Errorf ("%s" , strings .TrimSpace (string (body )))
700+ }
704701
705- body , err := io .ReadAll (resp .Body )
706- if err != nil {
707- return "" , fmt .Errorf ("failed to read response body: %w" , err )
702+ resp .Body .Close ()
703+ if streaming {
704+ return nil , nil , fmt .Errorf ("stream request failed with status: %d" , resp .StatusCode )
705+ }
706+ return nil , nil , fmt .Errorf ("failed to list requests: %s" , resp .Status )
708707 }
709708
710- if resp .StatusCode == http .StatusNotFound {
711- return "" , fmt .Errorf ("%s" , strings .TrimSpace (string (body )))
709+ // Return the response body and a cancel function that closes it.
710+ cancel := func () {
711+ resp .Body .Close ()
712712 }
713713
714- return string ( body ) , nil
714+ return resp . Body , cancel , nil
715715}
716716
717717// doRequest is a helper function that performs HTTP requests and handles 503 responses
0 commit comments