use http.DefaultClient (#2530)

default client already handles proxy
This commit is contained in:
Michael Yang 2024-02-20 15:34:47 -08:00 committed by GitHub
parent 4613a080e7
commit 897b213468
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 52 deletions

View file

@ -21,7 +21,7 @@ import (
type Client struct { type Client struct {
base *url.URL base *url.URL
http http.Client http *http.Client
} }
func checkError(resp *http.Response, body []byte) error { func checkError(resp *http.Response, body []byte) error {
@ -66,30 +66,13 @@ func ClientFromEnvironment() (*Client, error) {
} }
} }
client := Client{ return &Client{
base: &url.URL{ base: &url.URL{
Scheme: scheme, Scheme: scheme,
Host: net.JoinHostPort(host, port), Host: net.JoinHostPort(host, port),
}, },
} http: http.DefaultClient,
}, nil
mockRequest, err := http.NewRequest(http.MethodHead, client.base.String(), nil)
if err != nil {
return nil, err
}
proxyURL, err := http.ProxyFromEnvironment(mockRequest)
if err != nil {
return nil, err
}
client.http = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
return &client, nil
} }
func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error { func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error {

View file

@ -34,20 +34,6 @@ type UpdateResponse struct {
UpdateVersion string `json:"version"` UpdateVersion string `json:"version"`
} }
func getClient(req *http.Request) http.Client {
proxyURL, err := http.ProxyFromEnvironment(req)
if err != nil {
slog.Warn(fmt.Sprintf("failed to handle proxy: %s", err))
return http.Client{}
}
return http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
}
func IsNewReleaseAvailable(ctx context.Context) (bool, UpdateResponse) { func IsNewReleaseAvailable(ctx context.Context) (bool, UpdateResponse) {
var updateResp UpdateResponse var updateResp UpdateResponse
@ -83,10 +69,9 @@ func IsNewReleaseAvailable(ctx context.Context) (bool, UpdateResponse) {
} }
req.Header.Set("Authorization", signature) req.Header.Set("Authorization", signature)
req.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version())) req.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version()))
client := getClient(req)
slog.Debug("checking for available update", "requestURL", requestURL) slog.Debug("checking for available update", "requestURL", requestURL)
resp, err := client.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
slog.Warn(fmt.Sprintf("failed to check for update: %s", err)) slog.Warn(fmt.Sprintf("failed to check for update: %s", err))
return false, updateResp return false, updateResp
@ -119,8 +104,8 @@ func DownloadNewRelease(ctx context.Context, updateResp UpdateResponse) error {
if err != nil { if err != nil {
return err return err
} }
client := getClient(req)
resp, err := client.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return fmt.Errorf("error checking update: %w", err) return fmt.Errorf("error checking update: %w", err)
} }
@ -151,7 +136,7 @@ func DownloadNewRelease(ctx context.Context, updateResp UpdateResponse) error {
cleanupOldDownloads() cleanupOldDownloads()
req.Method = http.MethodGet req.Method = http.MethodGet
resp, err = client.Do(req) resp, err = http.DefaultClient.Do(req)
if err != nil { if err != nil {
return fmt.Errorf("error checking update: %w", err) return fmt.Errorf("error checking update: %w", err)
} }

View file

@ -1103,18 +1103,7 @@ func makeRequest(ctx context.Context, method string, requestURL *url.URL, header
req.ContentLength = contentLength req.ContentLength = contentLength
} }
proxyURL, err := http.ProxyFromEnvironment(req) resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
client := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
},
}
resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }