mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2026-01-06 07:04:25 +08:00
fix: Resolve installation failures of OpenResty on low-memory machines. (#10697)
This commit is contained in:
parent
79d9d9dfc2
commit
5b86ca38e0
2 changed files with 29 additions and 4 deletions
|
|
@ -2,7 +2,6 @@ package files
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/1Panel-dev/1Panel/agent/app/dto"
|
||||
"github.com/1Panel-dev/1Panel/agent/buserr"
|
||||
|
|
@ -255,10 +254,12 @@ func IsEmptyDir(dir string) bool {
|
|||
}
|
||||
|
||||
func DownloadFileWithProxy(url, dst string) error {
|
||||
_, resp, err := req_helper.HandleRequest(url, http.MethodGet, constant.TimeOut5m)
|
||||
resp, cancel, err := req_helper.RequestFile(url, http.MethodGet, constant.TimeOut5m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cancel()
|
||||
defer resp.Close()
|
||||
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
|
|
@ -266,8 +267,7 @@ func DownloadFileWithProxy(url, dst string) error {
|
|||
}
|
||||
defer out.Close()
|
||||
|
||||
reader := bytes.NewReader(resp)
|
||||
if _, err = io.Copy(out, reader); err != nil {
|
||||
if _, err = io.Copy(out, resp); err != nil {
|
||||
return fmt.Errorf("save download file [%s] error, err %s", dst, err.Error())
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -78,6 +78,31 @@ func HandleRequest(url, method string, timeout int) (int, []byte, error) {
|
|||
return resp.StatusCode, body, nil
|
||||
}
|
||||
|
||||
func RequestFile(url, method string, timeout int) (io.ReadCloser, context.CancelFunc, error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
global.LOG.Errorf("handle request failed, error message: %v", r)
|
||||
return
|
||||
}
|
||||
}()
|
||||
transport := xpack.LoadRequestTransport()
|
||||
client := http.Client{Timeout: time.Duration(timeout) * time.Second, Transport: transport}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
||||
request, err := http.NewRequestWithContext(ctx, method, url, nil)
|
||||
if err != nil {
|
||||
return nil, cancel, err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
resp, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, cancel, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, cancel, errors.New(resp.Status)
|
||||
}
|
||||
return resp.Body, cancel, nil
|
||||
}
|
||||
|
||||
func loadRequestTransport() *http.Transport {
|
||||
return &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue