Skip to content

Commit

Permalink
refactor: fallback to connecting ip
Browse files Browse the repository at this point in the history
close #142
  • Loading branch information
naiba committed Dec 19, 2024
1 parent 189da33 commit 1278430
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 @@ -23,8 +23,9 @@ var (
CustomEndpoints []string
GeoQueryIP, CachedCountryCode string
GeoQueryIPChanged bool = true
httpClientV4 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
httpClientV6 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
retryTimes int
httpClientV4 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
httpClientV6 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
)

// UpdateIP 按设置时间间隔更新IP地址的缓存
Expand Down Expand Up @@ -60,13 +61,27 @@ func FetchIP(useIPv6CountryCode bool) *pb.GeoIP {
}

if GeoQueryIP != "" {
retryTimes = 0
return &pb.GeoIP{
Use6: useIPv6CountryCode,
Ip: &pb.IP{
Ipv4: ipv4,
Ipv6: ipv6,
},
}
} else {
if retryTimes < 3 {
retryTimes++
} else {
// fallback to connecting IP
return &pb.GeoIP{
Use6: false,
Ip: &pb.IP{
Ipv4: "",
Ipv6: "",
},
}
}
}

return nil
Expand Down

0 comments on commit 1278430

Please sign in to comment.