Skip to content

Commit

Permalink
fix(go): 配置go测试网络环境超时时间
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhaoxuan authored and iseki0 committed Jan 20, 2025
1 parent e22f049 commit 3512d68
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions module/go_mod/gotree.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/Masterminds/semver"
"github.com/murphysecurity/murphysec/infra/logctx"
Expand All @@ -21,34 +22,36 @@ import (

func checkNetworkEnvironment(ctx context.Context) bool {
var goProxy []string

if strings.Contains(os.Getenv("GOPROXY"), ",") {
goProxy = strings.Split(os.Getenv("GOPROXY"), ",")
} else {
goProxy = append(goProxy, os.Getenv("GOPROXY"))
}
networkEnvironment := false
timeout := 10 * time.Second
client := http.Client{
Timeout: timeout, // 使用设置的超时时间
}
for range 3 {
for _, j := range goProxy {
if j == "direct" {
continue
}
r, e := http.Get(j)
r, e := client.Get(j)
if e != nil {
logctx.Use(ctx).Warn("test network environment http get error :" + e.Error())
continue
}
if r != nil && r.StatusCode == http.StatusRequestTimeout {
logctx.Use(ctx).Warn("test network environment http get timeout :" + j)
r.Body.Close()
continue
}
if e != nil {
logctx.Use(ctx).Warn("test network environment http get error :" + e.Error())
continue
}
logctx.Use(ctx).Debug("test network environment http get success :" + j)
networkEnvironment = true
break
r.Body.Close()
return true
}
}
return networkEnvironment
return false
}
func goModTidy(ctx context.Context, path string) error {
logger := logctx.Use(ctx)
Expand Down

0 comments on commit 3512d68

Please sign in to comment.