fix: 增加系统请求超时时间 (#5324)

This commit is contained in:
ssongliu 2024-06-06 17:37:38 +08:00 committed by GitHub
parent f670095c08
commit 135ac91160
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 29 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,3 +12,9 @@ const (
TypeSSL = "ssl"
TypeSystem = "system"
)
const (
TimeOut5s = 5
TimeOut20s = 20
TimeOut5m = 300
)

View File

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

View File

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