1
0
mirror of https://github.com/rancher/os.git synced 2025-07-11 13:53:04 +00:00

Use retry httpclient for LoadFromNetwork

This commit is contained in:
niusmallnan 2019-08-15 16:32:37 +08:00 committed by niusmallnan
parent 3961aa6855
commit 4841467d41

View File

@ -4,22 +4,17 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"strings" "strings"
"time"
"github.com/rancher/os/config" "github.com/rancher/os/config"
httpRetry "github.com/rancher/os/config/cloudinit/pkg"
"github.com/rancher/os/pkg/log" "github.com/rancher/os/pkg/log"
yaml "github.com/cloudfoundry-incubator/candiedyaml" yaml "github.com/cloudfoundry-incubator/candiedyaml"
composeConfig "github.com/docker/libcompose/config" composeConfig "github.com/docker/libcompose/config"
) )
const (
defaultTimeout = 10 * time.Second
)
var ( var (
ErrNoNetwork = errors.New("Networking not available to load resource") ErrNoNetwork = errors.New("Networking not available to load resource")
ErrNotFound = errors.New("Failed to find resource") ErrNotFound = errors.New("Failed to find resource")
@ -113,29 +108,18 @@ func LoadFromNetwork(location string) ([]byte, error) {
} }
SetProxyEnvironmentVariables() SetProxyEnvironmentVariables()
var resp *http.Response client := httpRetry.NewHTTPClient()
log.Debugf("LoadFromNetwork(%s)", location) client.MaxRetries = 3
netClient := &http.Client{ log.Debugf("start trying LoadFromNetwork(%s)", location)
Timeout: defaultTimeout, bytes, err := client.GetRetry(location)
}
resp, err = netClient.Get(location)
log.Debugf("LoadFromNetwork(%s) returned %v, %v", location, resp, err)
if err == nil {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("non-200 http response: %d", resp.StatusCode)
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Errorf("failed to LoadFromNetwork: %v", err)
return nil, err return nil, err
} }
log.Debugf("LoadFromNetwork(%s) returned", location)
cacheAdd(location, bytes) cacheAdd(location, bytes)
return bytes, nil
}
return nil, err return bytes, nil
} }
func LoadResource(location string, network bool) ([]byte, error) { func LoadResource(location string, network bool) ([]byte, error) {