From 1278430792dbe755c1d9232ffc2f3994050c744a Mon Sep 17 00:00:00 2001 From: naiba Date: Thu, 19 Dec 2024 23:18:24 +0800 Subject: [PATCH] refactor: fallback to connecting ip close #142 --- pkg/monitor/myip.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/monitor/myip.go b/pkg/monitor/myip.go index 9396b739..14922ec3 100644 --- a/pkg/monitor/myip.go +++ b/pkg/monitor/myip.go @@ -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地址的缓存 @@ -60,6 +61,7 @@ func FetchIP(useIPv6CountryCode bool) *pb.GeoIP { } if GeoQueryIP != "" { + retryTimes = 0 return &pb.GeoIP{ Use6: useIPv6CountryCode, Ip: &pb.IP{ @@ -67,6 +69,19 @@ func FetchIP(useIPv6CountryCode bool) *pb.GeoIP { Ipv6: ipv6, }, } + } else { + if retryTimes < 3 { + retryTimes++ + } else { + // fallback to connecting IP + return &pb.GeoIP{ + Use6: false, + Ip: &pb.IP{ + Ipv4: "", + Ipv6: "", + }, + } + } } return nil