Skip to content

Commit 3512d68

Browse files
chenhaoxuaniseki0
authored andcommitted
fix(go): 配置go测试网络环境超时时间
1 parent e22f049 commit 3512d68

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

module/go_mod/gotree.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"strings"
13+
"time"
1314

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

2223
func checkNetworkEnvironment(ctx context.Context) bool {
2324
var goProxy []string
24-
2525
if strings.Contains(os.Getenv("GOPROXY"), ",") {
2626
goProxy = strings.Split(os.Getenv("GOPROXY"), ",")
2727
} else {
2828
goProxy = append(goProxy, os.Getenv("GOPROXY"))
2929
}
30-
networkEnvironment := false
30+
timeout := 10 * time.Second
31+
client := http.Client{
32+
Timeout: timeout, // 使用设置的超时时间
33+
}
3134
for range 3 {
3235
for _, j := range goProxy {
3336
if j == "direct" {
3437
continue
3538
}
36-
r, e := http.Get(j)
39+
r, e := client.Get(j)
40+
if e != nil {
41+
logctx.Use(ctx).Warn("test network environment http get error :" + e.Error())
42+
continue
43+
}
3744
if r != nil && r.StatusCode == http.StatusRequestTimeout {
3845
logctx.Use(ctx).Warn("test network environment http get timeout :" + j)
3946
r.Body.Close()
4047
continue
4148
}
42-
if e != nil {
43-
logctx.Use(ctx).Warn("test network environment http get error :" + e.Error())
44-
continue
45-
}
4649
logctx.Use(ctx).Debug("test network environment http get success :" + j)
47-
networkEnvironment = true
48-
break
50+
r.Body.Close()
51+
return true
4952
}
5053
}
51-
return networkEnvironment
54+
return false
5255
}
5356
func goModTidy(ctx context.Context, path string) error {
5457
logger := logctx.Use(ctx)

0 commit comments

Comments
 (0)