An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Automatically refresh local DNS cache and retry when data sync fails due to DNS issues on Windows https://github.com/siyuan-note/siyuan/issues/17936
Danielfffcab6699fb
diff --git a/kernel/model/flushdns_other.go b/kernel/model/flushdns_other.gonew file mode 100644index 0000000000..0c43c522a7--- /dev/null+++ b/kernel/model/flushdns_other.go@@ -0,0 +1,23 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++//go:build !windows++package model++// flushDNS 非 Windows 平台暂不支持刷新系统 DNS 缓存。+func flushDNS() {+}diff --git a/kernel/model/flushdns_windows.go b/kernel/model/flushdns_windows.gonew file mode 100644index 0000000000..815b28b0b9--- /dev/null+++ b/kernel/model/flushdns_windows.go@@ -0,0 +1,39 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++//go:build windows++package model++import (+ "os/exec"++ "github.com/88250/gulu"+ "github.com/siyuan-note/logging"+)++// flushDNS 刷新 Windows 系统 DNS 解析缓存,用于在同步遇到 DNS 类错误(域名解析失败、过期缓存)后+// 清掉本地可能过期的解析记录,以便后续重试时能重新向上游 DNS 查询。+func flushDNS() {+ cmd := exec.Command("ipconfig", "/flushdns")+ gulu.CmdAttr(cmd)+ output, err := cmd.CombinedOutput()+ if nil != err {+ logging.LogErrorf("flush DNS cache failed: %s", err)+ return+ }+ logging.LogInfof("flushed DNS cache: %s", gulu.DecodeCmdOutput(output))+}diff --git a/kernel/model/repository.go b/kernel/model/repository.goindex da6f91867d..3cc5c3b26e 100644--- a/kernel/model/repository.go+++ b/kernel/model/repository.go@@ -1726,7 +1726,7 @@ func bootSyncRepo() (err error) {if 0 < len(fetchedFiles) {go func() {- _, syncErr := syncRepo(false, false)+ _, syncErr := syncRepoWithDNSRetry(false, false)isBootSyncing.Store(false)if err != nil {logging.LogErrorf("boot background sync repo failed: %s", syncErr)diff --git a/kernel/model/sync.go b/kernel/model/sync.goindex a4248bed87..753a60f28c 100644--- a/kernel/model/sync.go+++ b/kernel/model/sync.go@@ -62,7 +62,7 @@ func SyncDataDownload() {now := util.CurrentTimeMillis()Conf.Sync.Synced = now- err := syncRepoDownload()+ err := syncRepoDownloadWithDNSRetry()code := 1if err != nil {code = 2@@ -92,7 +92,7 @@ func SyncDataUpload() {now := util.CurrentTimeMillis()Conf.Sync.Synced = now- err := syncRepoUpload()+ err := syncRepoUploadWithDNSRetry()code := 1if err != nil {code = 2@@ -202,7 +202,7 @@ func syncData(exit, byHand bool) {now := util.CurrentTimeMillis()Conf.Sync.Synced = now- dataChanged, err := syncRepo(exit, byHand)+ dataChanged, err := syncRepoWithDNSRetry(exit, byHand)code := 1if err != nil {code = 2@@ -660,7 +660,7 @@ func formatRepoErrorMsg(err error) string {msg = fmt.Sprintf(Conf.Language(85), err)} else if strings.Contains(msgLowerCase, "cipher: message authentication failed") {msg = Conf.Language(135)- } else if strings.Contains(msgLowerCase, "no such host") || strings.Contains(msgLowerCase, "connection failed") || strings.Contains(msgLowerCase, "hostname resolution") || strings.Contains(msgLowerCase, "No address associated with hostname") {+ } else if isDNSError(msgLowerCase) {msg = Conf.Language(24)} else if strings.Contains(msgLowerCase, "net/http: request canceled while waiting for connection") || strings.Contains(msgLowerCase, "exceeded while awaiting") || strings.Contains(msgLowerCase, "context deadline exceeded") || strings.Contains(msgLowerCase, "timeout") || strings.Contains(msgLowerCase, "context cancellation while reading body") {msg = Conf.Language(24)@@ -672,6 +672,70 @@ func formatRepoErrorMsg(err error) string {return msg}+// isDNSError 判断错误信息是否属于 DNS 解析类(域名解析失败、主机名无法解析等)。+// dejavu/cloud 层用 fmt.Errorf 原样透传底层网络错误,因此这里用字符串匹配兜底。+func isDNSError(msg string) bool {+ return strings.Contains(msg, "no such host") ||+ strings.Contains(msg, "connection failed") ||+ strings.Contains(msg, "hostname resolution") ||+ strings.Contains(msg, "no address associated with hostname")+}++// lastDNSFlushTime 及其锁用于 DNS 刷新节流,避免自动同步循环里反复 fork 系统命令。+// 由于同步可能从多个入口并发执行(如启动后台同步与手动同步),这里用互斥锁保护。+var (+ lastDNSFlushTime time.Time+ lastDNSFlushTimeMu sync.Mutex+)++// flushAndRetryOnDNSError 在确认是 DNS 类错误且距上次刷新超过 5 分钟时,刷新系统 DNS 缓存并返回 true+// 以触发上层重试一次同步;否则返回 false。节流是为了避免高频自动同步反复 fork 系统命令。+func flushAndRetryOnDNSError(err error) bool {+ if !isDNSError(strings.ToLower(err.Error())) {+ return false+ }++ lastDNSFlushTimeMu.Lock()+ defer lastDNSFlushTimeMu.Unlock()+ if time.Since(lastDNSFlushTime) < 5*time.Minute {+ logging.LogInfof("sync failed with DNS error, but DNS cache was flushed recently, skip retry")+ return false+ }+ lastDNSFlushTime = time.Now()++ logging.LogInfof("sync failed with DNS error [%s], flushing DNS cache and retrying once", err)+ flushDNS()+ return true+}++// syncRepoWithDNSRetry 执行一次同步,若失败且判定为 DNS 类错误,则刷新系统 DNS 缓存后重试一次。+// 统一封装 DNS 重试逻辑,供主同步流程(syncData)与启动后台同步复用。+func syncRepoWithDNSRetry(exit, byHand bool) (dataChanged bool, err error) {+ dataChanged, err = syncRepo(exit, byHand)+ if nil != err && flushAndRetryOnDNSError(err) {+ dataChanged, err = syncRepo(exit, byHand)+ }+ return+}++// syncRepoDownloadWithDNSRetry 仅下载同步,DNS 类错误时刷新系统 DNS 缓存后重试一次。+func syncRepoDownloadWithDNSRetry() (err error) {+ err = syncRepoDownload()+ if nil != err && flushAndRetryOnDNSError(err) {+ err = syncRepoDownload()+ }+ return+}++// syncRepoUploadWithDNSRetry 仅上传同步,DNS 类错误时刷新系统 DNS 缓存后重试一次。+func syncRepoUploadWithDNSRetry() (err error) {+ err = syncRepoUpload()+ if nil != err && flushAndRetryOnDNSError(err) {+ err = syncRepoUpload()+ }+ return+}+func getSyncIgnoreLines() (ret []string) {ignore := filepath.Join(util.DataDir, ".siyuan", "syncignore")err := os.MkdirAll(filepath.Dir(ignore), 0755)