From de1c01a2e8dc4c0b78285f30277512ba958a75a9 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 4 Apr 2023 10:21:34 +0800 Subject: [PATCH] :art: Add retry to improve network check --- kernel/model/sync.go | 2 +- kernel/util/net.go | 27 ++++++++++----------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/kernel/model/sync.go b/kernel/model/sync.go index 3da736214..94afe9ba5 100644 --- a/kernel/model/sync.go +++ b/kernel/model/sync.go @@ -576,7 +576,7 @@ func isProviderOnline() (ret bool) { checkURL = Conf.Sync.WebDAV.Endpoint default: logging.LogWarnf("unknown provider: %d", Conf.Sync.Provider) - util.IsOnline("") + return false } if ret = util.IsOnline(checkURL); !ret { diff --git a/kernel/util/net.go b/kernel/util/net.go index 7da8f1e38..b1c550dfe 100644 --- a/kernel/util/net.go +++ b/kernel/util/net.go @@ -33,19 +33,6 @@ import ( func IsOnline(checkURL string) bool { if "" == checkURL { - if isOnline("https://www.baidu.com") { - return true - } - - if isOnline("https://icanhazip.com") { - return true - } - - if isOnline("https://api.ipify.org") { - return true - } - - logging.LogWarnf("network is offline") return false } @@ -57,10 +44,16 @@ func IsOnline(checkURL string) bool { return false } -func isOnline(checkURL string) bool { - c := req.C().SetTimeout(1 * time.Second) - _, err := c.R().Head(checkURL) - return nil == err +func isOnline(checkURL string) (ret bool) { + for i := 0; i < 3; i++ { + c := req.C().SetTimeout(3 * time.Second) + _, err := c.R().Head(checkURL) + ret = nil == err + if ret { + break + } + } + return } func GetRemoteAddr(session *melody.Session) string {