1
0
mirror of https://github.com/rancher/os.git synced 2025-07-10 13:23:03 +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"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/rancher/os/config"
httpRetry "github.com/rancher/os/config/cloudinit/pkg"
"github.com/rancher/os/pkg/log"
yaml "github.com/cloudfoundry-incubator/candiedyaml"
composeConfig "github.com/docker/libcompose/config"
)
const (
defaultTimeout = 10 * time.Second
)
var (
ErrNoNetwork = errors.New("Networking not available to load resource")
ErrNotFound = errors.New("Failed to find resource")
@ -113,29 +108,18 @@ func LoadFromNetwork(location string) ([]byte, error) {
}
SetProxyEnvironmentVariables()
var resp *http.Response
log.Debugf("LoadFromNetwork(%s)", location)
netClient := &http.Client{
Timeout: defaultTimeout,
client := httpRetry.NewHTTPClient()
client.MaxRetries = 3
log.Debugf("start trying LoadFromNetwork(%s)", location)
bytes, err := client.GetRetry(location)
if err != nil {
log.Errorf("failed to LoadFromNetwork: %v", err)
return nil, err
}
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)
}
log.Debugf("LoadFromNetwork(%s) returned", location)
cacheAdd(location, bytes)
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
cacheAdd(location, bytes)
return bytes, nil
}
return nil, err
return bytes, nil
}
func LoadResource(location string, network bool) ([]byte, error) {