on main help sign in
perf: fix possibly unclosed resp.body (#1818)
b884767Carlos Alexandro Becker
internal/config/config.go+7−7
@@ -806,21 +806,21 @@ func (c *ProviderConfig) TestConnection(resolver VariableResolver) error {
806 for k, v := range c.ExtraHeaders {
806 for k, v := range c.ExtraHeaders {
807 req.Header.Set(k, v)
807 req.Header.Set(k, v)
808 }
808 }
809− b, err := client.Do(req)
809+ resp, err := client.Do(req)
810 if err != nil {
810 if err != nil {
811 return fmt.Errorf("failed to create request for provider %s: %w", c.ID, err)
811 return fmt.Errorf("failed to create request for provider %s: %w", c.ID, err)
812 }
812 }
813+ defer resp.Body.Close()
813 if c.ID == string(catwalk.InferenceProviderZAI) {
814 if c.ID == string(catwalk.InferenceProviderZAI) {
814− if b.StatusCode == http.StatusUnauthorized {
815+ if resp.StatusCode == http.StatusUnauthorized {
815− // for z.ai just check if the http response is not 401
816+ // For z.ai just check if the http response is not 401.
816− return fmt.Errorf("failed to connect to provider %s: %s", c.ID, b.Status)
817+ return fmt.Errorf("failed to connect to provider %s: %s", c.ID, resp.Status)
817 }
818 }
818 } else {
819 } else {
819− if b.StatusCode != http.StatusOK {
820+ if resp.StatusCode != http.StatusOK {
820− return fmt.Errorf("failed to connect to provider %s: %s", c.ID, b.Status)
821+ return fmt.Errorf("failed to connect to provider %s: %s", c.ID, resp.Status)
821 }
822 }
822 }
823 }
823− _ = b.Body.Close()
824 return nil
824 return nil
825}
825}
826
826