improve: 提高单栈机器 IP 获取速度
This commit is contained in:
parent
9cdd941a97
commit
2083512607
@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nezhahq/agent/pkg/util"
|
||||
@ -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 {
|
||||
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user