improve: 提高单栈机器 IP 获取速度
This commit is contained in:
parent
9cdd941a97
commit
2083512607
@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nezhahq/agent/pkg/util"
|
"github.com/nezhahq/agent/pkg/util"
|
||||||
@ -27,8 +28,18 @@ var (
|
|||||||
func UpdateIP(useIPv6CountryCode bool, period uint32) {
|
func UpdateIP(useIPv6CountryCode bool, period uint32) {
|
||||||
for {
|
for {
|
||||||
util.Println(agentConfig.Debug, "正在更新本地缓存IP信息")
|
util.Println(agentConfig.Debug, "正在更新本地缓存IP信息")
|
||||||
ipv4 := fetchIP(cfList, false)
|
wg := new(sync.WaitGroup)
|
||||||
ipv6 := fetchIP(cfList, true)
|
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 ipv4 == "" && ipv6 == "" {
|
||||||
if period > 60 {
|
if period > 60 {
|
||||||
@ -67,6 +78,10 @@ func fetchIP(servers []string, isV6 bool) string {
|
|||||||
} else {
|
} else {
|
||||||
resp, err = httpGetWithUA(httpClientV4, servers[i])
|
resp, err = httpGetWithUA(httpClientV4, servers[i])
|
||||||
}
|
}
|
||||||
|
// 遇到单栈机器提前退出
|
||||||
|
if err != nil && strings.Contains(err.Error(), "no route to host") {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user