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:
parent
3961aa6855
commit
4841467d41
@ -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)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to LoadFromNetwork: %v", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
resp, err = netClient.Get(location)
|
log.Debugf("LoadFromNetwork(%s) returned", location)
|
||||||
log.Debugf("LoadFromNetwork(%s) returned %v, %v", location, resp, err)
|
cacheAdd(location, bytes)
|
||||||
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)
|
return bytes, nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheAdd(location, bytes)
|
|
||||||
return bytes, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadResource(location string, network bool) ([]byte, error) {
|
func LoadResource(location string, network bool) ([]byte, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user