fix: Resolve installation failures of OpenResty on low-memory machines. (#10697)

This commit is contained in:
CityFun 2025-10-20 18:17:38 +08:00 committed by GitHub
parent 79d9d9dfc2
commit 5b86ca38e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 4 deletions

View file

@ -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

View file

@ -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},