From 135ac91160c6a34b17a7dfaa7875cc19d1d070d8 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 6 Jun 2024 17:37:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=20(#5324?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/app.go | 8 ++++---- backend/app/service/app_install.go | 11 ++++++----- backend/app/service/app_utils.go | 4 ++-- backend/app/service/runtime_utils.go | 2 +- backend/app/service/upgrade.go | 5 +++-- backend/constant/common.go | 6 ++++++ backend/utils/files/file_op.go | 3 ++- backend/utils/http/request.go | 10 +++++----- 8 files changed, 29 insertions(+), 20 deletions(-) diff --git a/backend/app/service/app.go b/backend/app/service/app.go index b93ad56d8..94f263bcf 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -234,7 +234,7 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response. if appDetailDTO.DockerCompose == "" { filename := filepath.Base(appDetailDTO.DownloadUrl) dockerComposeUrl := fmt.Sprintf("%s%s", strings.TrimSuffix(appDetailDTO.DownloadUrl, filename), "docker-compose.yml") - statusCode, composeRes, err := httpUtil.HandleGet(dockerComposeUrl, http.MethodGet) + statusCode, composeRes, err := httpUtil.HandleGet(dockerComposeUrl, http.MethodGet, constant.TimeOut20s) if err != nil { return appDetailDTO, buserr.WithDetail("ErrGetCompose", err.Error(), err) } @@ -710,7 +710,7 @@ func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) { } versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.CONF.System.AppRepo, global.CONF.System.Mode) - _, versionRes, err := http2.HandleGet(versionUrl, http.MethodGet) + _, versionRes, err := http2.HandleGet(versionUrl, http.MethodGet, constant.TimeOut20s) if err != nil { return nil, err } @@ -847,7 +847,7 @@ func (a AppService) SyncAppListFromRemote() (err error) { global.LOG.Infof("Starting synchronization of application details...") for _, l := range list.Apps { app := appsMap[l.AppProperty.Key] - _, iconRes, err := httpUtil.HandleGetWithTransport(l.Icon, http.MethodGet, transport) + _, iconRes, err := httpUtil.HandleGetWithTransport(l.Icon, http.MethodGet, transport, constant.TimeOut20s) if err != nil { return err } @@ -874,7 +874,7 @@ func (a AppService) SyncAppListFromRemote() (err error) { if _, ok := InitTypes[app.Type]; ok { dockerComposeUrl := fmt.Sprintf("%s/%s", versionUrl, "docker-compose.yml") - _, composeRes, err := httpUtil.HandleGetWithTransport(dockerComposeUrl, http.MethodGet, transport) + _, composeRes, err := httpUtil.HandleGetWithTransport(dockerComposeUrl, http.MethodGet, transport, constant.TimeOut20s) if err != nil { return err } diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index 693f9fe83..2584b35d1 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -4,10 +4,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/1Panel-dev/1Panel/backend/utils/files" - httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http" - "github.com/docker/docker/api/types" - "gopkg.in/yaml.v3" "math" "net/http" "os" @@ -18,6 +14,11 @@ import ( "strconv" "strings" + "github.com/1Panel-dev/1Panel/backend/utils/files" + httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http" + "github.com/docker/docker/api/types" + "gopkg.in/yaml.v3" + "github.com/1Panel-dev/1Panel/backend/utils/env" "github.com/1Panel-dev/1Panel/backend/utils/nginx" "github.com/joho/godotenv" @@ -520,7 +521,7 @@ func (a *AppInstallService) GetUpdateVersions(req request.AppUpdateVersion) ([]d if req.UpdateVersion != "" && req.UpdateVersion == detail.Version && detail.DockerCompose == "" && !app.IsLocalApp() { filename := filepath.Base(detail.DownloadUrl) dockerComposeUrl := fmt.Sprintf("%s%s", strings.TrimSuffix(detail.DownloadUrl, filename), "docker-compose.yml") - statusCode, composeRes, err := httpUtil.HandleGet(dockerComposeUrl, http.MethodGet) + statusCode, composeRes, err := httpUtil.HandleGet(dockerComposeUrl, http.MethodGet, constant.TimeOut20s) if err != nil { return versions, err } diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 2b893f5dc..d7f8b3563 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -561,7 +561,7 @@ func upgradeInstall(req request.AppInstallUpgrade) error { _ = appDetailRepo.Update(context.Background(), detail) } go func() { - _, _, _ = httpUtil.HandleGet(detail.DownloadCallBackUrl, http.MethodGet) + _, _, _ = httpUtil.HandleGet(detail.DownloadCallBackUrl, http.MethodGet, constant.TimeOut5s) }() } @@ -803,7 +803,7 @@ func copyData(app model.App, appDetail model.AppDetail, appInstall *model.AppIns return } go func() { - _, _, _ = httpUtil.HandleGet(appDetail.DownloadCallBackUrl, http.MethodGet) + _, _, _ = httpUtil.HandleGet(appDetail.DownloadCallBackUrl, http.MethodGet, constant.TimeOut5s) }() } appKey := app.Key diff --git a/backend/app/service/runtime_utils.go b/backend/app/service/runtime_utils.go index bcf2bcf56..5b87b6315 100644 --- a/backend/app/service/runtime_utils.go +++ b/backend/app/service/runtime_utils.go @@ -55,7 +55,7 @@ func handleNode(create request.RuntimeCreate, runtime *model.Runtime, fileOp fil } go func() { - if _ , _, err := httpUtil.HandleGet(nodeDetail.DownloadCallBackUrl, http.MethodGet); err != nil { + if _, _, err := httpUtil.HandleGet(nodeDetail.DownloadCallBackUrl, http.MethodGet, constant.TimeOut5s); err != nil { global.LOG.Errorf("http request failed(handleNode), err: %v", err) return } diff --git a/backend/app/service/upgrade.go b/backend/app/service/upgrade.go index 36f6e7fe3..cdcd92506 100644 --- a/backend/app/service/upgrade.go +++ b/backend/app/service/upgrade.go @@ -11,6 +11,7 @@ import ( "time" "github.com/1Panel-dev/1Panel/backend/app/dto" + "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common" @@ -258,7 +259,7 @@ func (u *UpgradeService) loadVersion(isLatest bool, currentVersion, mode string) if !isLatest { path = fmt.Sprintf("%s/%s/latest.current", global.CONF.System.RepoUrl, mode) } - _, latestVersionRes, err := httpUtil.HandleGet(path, http.MethodGet) + _, latestVersionRes, err := httpUtil.HandleGet(path, http.MethodGet, constant.TimeOut20s) if err != nil { global.LOG.Errorf("load latest version from oss failed, err: %v", err) return "" @@ -319,7 +320,7 @@ func (u *UpgradeService) checkVersion(v2, v1 string) string { } func (u *UpgradeService) loadReleaseNotes(path string) (string, error) { - _, releaseNotes, err := httpUtil.HandleGet(path, http.MethodGet) + _, releaseNotes, err := httpUtil.HandleGet(path, http.MethodGet, constant.TimeOut20s) if err != nil { return "", err } diff --git a/backend/constant/common.go b/backend/constant/common.go index 90574267c..80d6f1a74 100644 --- a/backend/constant/common.go +++ b/backend/constant/common.go @@ -12,3 +12,9 @@ const ( TypeSSL = "ssl" TypeSystem = "system" ) + +const ( + TimeOut5s = 5 + TimeOut20s = 20 + TimeOut5m = 300 +) diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 2595c2a32..236465a1d 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -18,6 +18,7 @@ import ( "strings" "time" + "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/utils/cmd" http2 "github.com/1Panel-dev/1Panel/backend/utils/http" cZip "github.com/klauspost/compress/zip" @@ -329,7 +330,7 @@ func (f FileOp) DownloadFile(url, dst string) error { } func (f FileOp) DownloadFileWithProxy(url, dst string) error { - _, resp, err := http2.HandleGet(url, http.MethodGet) + _, resp, err := http2.HandleGet(url, http.MethodGet, constant.TimeOut5m) if err != nil { return err } diff --git a/backend/utils/http/request.go b/backend/utils/http/request.go index 1a36cfdcd..6445ec8ef 100644 --- a/backend/utils/http/request.go +++ b/backend/utils/http/request.go @@ -10,12 +10,12 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/xpack" ) -func HandleGet(url, method string) (int, []byte, error) { +func HandleGet(url, method string, timeout int) (int, []byte, error) { transport := xpack.LoadRequestTransport() - return HandleGetWithTransport(url, method, transport) + return HandleGetWithTransport(url, method, transport, timeout) } -func HandleGetWithTransport(url, method string, transport *http.Transport) (int, []byte, error) { +func HandleGetWithTransport(url, method string, transport *http.Transport, timeout int) (int, []byte, error) { defer func() { if r := recover(); r != nil { global.LOG.Errorf("handle request failed, error message: %v", r) @@ -23,8 +23,8 @@ func HandleGetWithTransport(url, method string, transport *http.Transport) (int, } }() - client := http.Client{Timeout: 10 * time.Second, Transport: transport} - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + client := http.Client{Timeout: time.Duration(timeout) * time.Second, Transport: transport} + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) defer cancel() request, err := http.NewRequestWithContext(ctx, method, url, nil) if err != nil {