Skip to content

Commit

Permalink
improve: 提高单栈机器 IP 获取速度
Browse files Browse the repository at this point in the history
  • Loading branch information
naiba committed Aug 10, 2024
1 parent 9cdd941 commit 2083512
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/monitor/myip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"strings"
"sync"
"time"

"github.com/nezhahq/agent/pkg/util"
Expand All @@ -27,8 +28,18 @@ var (
func UpdateIP(useIPv6CountryCode bool, period uint32) {
for {
util.Println(agentConfig.Debug, "正在更新本地缓存IP信息")
ipv4 := fetchIP(cfList, false)
ipv6 := fetchIP(cfList, true)
wg := new(sync.WaitGroup)
wg.Add(2)
var ipv4, ipv6 string
go func() {
defer wg.Done()
ipv4 = fetchIP(cfList, false)
}()
go func() {
defer wg.Done()
ipv6 = fetchIP(cfList, true)
}()
wg.Wait()

if ipv4 == "" && ipv6 == "" {
if period > 60 {
Expand Down Expand Up @@ -67,6 +78,10 @@ func fetchIP(servers []string, isV6 bool) string {
} else {
resp, err = httpGetWithUA(httpClientV4, servers[i])
}
// 遇到单栈机器提前退出
if err != nil && strings.Contains(err.Error(), "no route to host") {
return ip
}
if err == nil {
body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down

0 comments on commit 2083512

Please sign in to comment.