mirror of
https://github.com/alibaba/higress.git
synced 2026-04-30 08:27:26 +08:00
feat:add installation for higress standalone in local docker environment (#606)
Co-authored-by: 澄潭 <zty98751@alibaba-inc.com>
This commit is contained in:
123
pkg/cmd/hgctl/util/http_fetcher.go
Normal file
123
pkg/cmd/hgctl/util/http_fetcher.go
Normal file
@@ -0,0 +1,123 @@
|
||||
// Copyright (c) 2022 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultInitialInterval = 500 * time.Millisecond
|
||||
defaultMaxInterval = 60 * time.Second
|
||||
)
|
||||
|
||||
type HTTPFetcher struct {
|
||||
client *http.Client
|
||||
initialBackoff time.Duration
|
||||
requestMaxRetry int
|
||||
bufferSize int64
|
||||
}
|
||||
|
||||
// NewHTTPFetcher create a new HTTP remote fetcher.
|
||||
func NewHTTPFetcher(requestTimeout time.Duration, requestMaxRetry int, bufferSize int64) *HTTPFetcher {
|
||||
if requestTimeout == 0 {
|
||||
requestTimeout = 5 * time.Second
|
||||
}
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
// nolint: gosec
|
||||
// This is only when a user explicitly sets a flag to enable insecure mode
|
||||
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
return &HTTPFetcher{
|
||||
client: &http.Client{
|
||||
Timeout: requestTimeout,
|
||||
},
|
||||
initialBackoff: defaultInitialInterval,
|
||||
requestMaxRetry: requestMaxRetry,
|
||||
bufferSize: bufferSize,
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch downloads with HTTP get.
|
||||
func (f *HTTPFetcher) Fetch(ctx context.Context, url string) ([]byte, error) {
|
||||
c := f.client
|
||||
delayInterval := f.initialBackoff
|
||||
attempts := 0
|
||||
var lastError error
|
||||
for attempts < f.requestMaxRetry {
|
||||
attempts++
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.Do(req)
|
||||
if err != nil {
|
||||
lastError = err
|
||||
if ctx.Err() != nil {
|
||||
// If there is context timeout, exit this loop.
|
||||
return nil, fmt.Errorf("download failed after %v attempts, last error: %v", attempts, lastError)
|
||||
}
|
||||
delayInterval = delayInterval + f.initialBackoff
|
||||
if delayInterval > defaultMaxInterval {
|
||||
break
|
||||
}
|
||||
time.Sleep(delayInterval)
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, f.bufferSize))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = resp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return body, err
|
||||
}
|
||||
|
||||
lastError = fmt.Errorf("download request failed: status code %v", resp.StatusCode)
|
||||
|
||||
if retryable(resp.StatusCode) {
|
||||
_, err := io.ReadAll(io.LimitReader(resp.Body, f.bufferSize))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = resp.Body.Close()
|
||||
delayInterval = delayInterval + f.initialBackoff
|
||||
if delayInterval > defaultMaxInterval {
|
||||
break
|
||||
}
|
||||
time.Sleep(delayInterval)
|
||||
continue
|
||||
}
|
||||
|
||||
err = resp.Body.Close()
|
||||
break
|
||||
|
||||
}
|
||||
return nil, fmt.Errorf("download failed after %v attempts, last error: %v", attempts, lastError)
|
||||
}
|
||||
|
||||
func retryable(code int) bool {
|
||||
return code >= 500 &&
|
||||
!(code == http.StatusNotImplemented ||
|
||||
code == http.StatusHTTPVersionNotSupported ||
|
||||
code == http.StatusNetworkAuthenticationRequired)
|
||||
}
|
||||
@@ -15,8 +15,10 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -76,3 +78,18 @@ func ParseValue(valueStr string) any {
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// WriteFileString write string content to file
|
||||
func WriteFileString(fileName string, content string, perm os.FileMode) error {
|
||||
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
writer := bufio.NewWriter(file)
|
||||
if _, err := writer.WriteString(content); err != nil {
|
||||
return err
|
||||
}
|
||||
writer.Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user