An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:zap: 云端数据同步时降低交互阻塞时间 Fix https://github.com/siyuan-note/siyuan/issues/4984
Liang Dingc88bd514d36a
diff --git a/kernel/model/backup.go b/kernel/model/backup.goindex 96338409d5..364586df92 100644--- a/kernel/model/backup.go+++ b/kernel/model/backup.go@@ -145,8 +145,6 @@ func RecoverLocalBackup() (err error) {data, _ = hex.DecodeString(string(data))passwd := string(data)- syncLock.Lock()- defer syncLock.Unlock()CloseWatchAssets()defer WatchAssets()@@ -233,7 +231,7 @@ func RecoverLocalBackup() (err error) {util.PushEndlessProgress(Conf.Language(62))time.Sleep(2 * time.Second)- refreshFileTree()+ RefreshFileTree()if syncEnabled {func() {time.Sleep(5 * time.Second)@@ -252,8 +250,6 @@ func CreateLocalBackup() (err error) {util.PushEndlessProgress(Conf.Language(22))WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()filesys.ReleaseAllFileLocks()@@ -326,9 +322,6 @@ func CreateLocalBackup() (err error) {}func DownloadBackup() (err error) {- syncLock.Lock()- defer syncLock.Unlock()-// 使用索引文件进行解密验证 https://github.com/siyuan-note/siyuan/issues/3789var tmpFetchedFiles intvar tmpTransferSize uint64@@ -367,9 +360,6 @@ func UploadBackup() (err error) {return}- syncLock.Lock()- defer syncLock.Unlock()-localDirPath := Conf.Backup.GetSaveDir()util.PushEndlessProgress(Conf.Language(61))util.LogInfof("uploading backup...")diff --git a/kernel/model/bazzar.go b/kernel/model/bazzar.goindex 37e7f3f987..956871efed 100644--- a/kernel/model/bazzar.go+++ b/kernel/model/bazzar.go@@ -50,8 +50,8 @@ func BazaarWidgets() (widgets []*bazaar.Widget) {}func InstallBazaarWidget(repoURL, repoHash, widgetName string) error {- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()installPath := filepath.Join(util.DataDir, "widgets", widgetName)err := bazaar.InstallWidget(repoURL, repoHash, installPath, Conf.System.NetworkProxy.String(), IsSubscriber(), Conf.System.ID)@@ -62,8 +62,8 @@ func InstallBazaarWidget(repoURL, repoHash, widgetName string) error {}func UninstallBazaarWidget(widgetName string) error {- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()installPath := filepath.Join(util.DataDir, "widgets", widgetName)err := bazaar.UninstallWidget(installPath)@@ -92,8 +92,8 @@ func BazaarIcons() (icons []*bazaar.Icon) {}func InstallBazaarIcon(repoURL, repoHash, iconName string) error {- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()installPath := filepath.Join(util.IconsPath, iconName)err := bazaar.InstallIcon(repoURL, repoHash, installPath, Conf.System.NetworkProxy.String(), IsSubscriber(), Conf.System.ID)@@ -107,8 +107,8 @@ func InstallBazaarIcon(repoURL, repoHash, iconName string) error {}func UninstallBazaarIcon(iconName string) error {- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()installPath := filepath.Join(util.IconsPath, iconName)err := bazaar.UninstallIcon(installPath)@@ -139,8 +139,8 @@ func BazaarThemes() (ret []*bazaar.Theme) {}func InstallBazaarTheme(repoURL, repoHash, themeName string, mode int, update bool) error {- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()closeThemeWatchers()@@ -167,8 +167,8 @@ func InstallBazaarTheme(repoURL, repoHash, themeName string, mode int, update bo}func UninstallBazaarTheme(themeName string) error {- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()closeThemeWatchers()@@ -198,8 +198,8 @@ func BazaarTemplates() (templates []*bazaar.Template) {}func InstallBazaarTemplate(repoURL, repoHash, templateName string) error {- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()installPath := filepath.Join(util.DataDir, "templates", templateName)err := bazaar.InstallTemplate(repoURL, repoHash, installPath, Conf.System.NetworkProxy.String(), IsSubscriber(), Conf.System.ID)@@ -210,8 +210,8 @@ func InstallBazaarTemplate(repoURL, repoHash, templateName string) error {}func UninstallBazaarTemplate(templateName string) error {- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()installPath := filepath.Join(util.DataDir, "templates", templateName)err := bazaar.UninstallTemplate(installPath)diff --git a/kernel/model/box.go b/kernel/model/box.goindex 6556180269..735fd474d2 100644--- a/kernel/model/box.go+++ b/kernel/model/box.go@@ -474,12 +474,7 @@ func parseKTree(kramdown []byte) (ret *parse.Tree) {func RefreshFileTree() {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()- refreshFileTree()-}-func refreshFileTree() {if err := sql.InitDatabase(true); nil != err {util.PushErrMsg(Conf.Language(85), 5000)returndiff --git a/kernel/model/export.go b/kernel/model/export.goindex 837b2bba19..1787444724 100644--- a/kernel/model/export.go+++ b/kernel/model/export.go@@ -74,8 +74,9 @@ func ExportDataInFolder(exportFolder string) (err error) {util.PushEndlessProgress(Conf.Language(65))defer util.ClearPushProgress(100)- syncLock.Lock()- defer syncLock.Unlock()+ WaitForWritingFiles()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()exportFolder = filepath.Join(exportFolder, util.CurrentTimeSecondsStr())err = exportData(exportFolder)@@ -89,8 +90,9 @@ func ExportData() (zipPath string) {util.PushEndlessProgress(Conf.Language(65))defer util.ClearPushProgress(100)- syncLock.Lock()- defer syncLock.Unlock()+ WaitForWritingFiles()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()baseFolderName := "data-" + util.CurrentTimeSecondsStr()exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)diff --git a/kernel/model/file.go b/kernel/model/file.goindex 5cb0e3a852..cc96473149 100644--- a/kernel/model/file.go+++ b/kernel/model/file.go@@ -841,9 +841,6 @@ func DuplicateDoc(rootID string) (err error) {defer util.PushClearMsg()WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()-tree, err := loadTreeByBlockID(rootID)if nil != err {return@@ -890,8 +887,6 @@ func DuplicateDoc(rootID string) (err error) {func CreateDocByMd(boxID, p, title, md string) (err error) {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()box := Conf.Box(boxID)if nil == box {@@ -911,9 +906,6 @@ func CreateWithMarkdown(boxID, hPath, md string) (id string, err error) {}WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()-luteEngine := NewLute()dom := luteEngine.Md2BlockDOM(md)id, err = createDocsByHPath(box.ID, hPath, dom)@@ -981,8 +973,8 @@ func MoveDoc(fromBoxID, fromPath, toBoxID, toPath string) (newPath string, err e}WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()tree, err := LoadTree(fromBoxID, fromPath)if nil != err {@@ -1109,8 +1101,8 @@ func RemoveDoc(boxID, p string) (err error) {}WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()tree, err := LoadTree(boxID, p)if nil != err {@@ -1241,8 +1233,6 @@ func CreateDailyNote(boxID string) (p string, err error) {}WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()existRoot := treenode.GetBlockTreeRootByHPath(box.ID, hPath)if nil != existRoot {@@ -1453,8 +1443,8 @@ func rootChildIDs(rootID string) (ret []string) {func ChangeFileTreeSort(boxID string, paths []string) {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()box := Conf.Box(boxID)sortIDs := map[string]int{}@@ -1609,8 +1599,6 @@ func (box *Box) removeSort(rootID, path string) {func ServeFile(c *gin.Context, filePath string) (err error) {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()if filesys.IsLocked(filePath) {if err = filesys.UnlockFile(filePath); nil == err {diff --git a/kernel/model/history.go b/kernel/model/history.goindex 1fd515f3c7..1583a8147a 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -50,9 +50,6 @@ func generateDocHistory() {}WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()-for _, box := range Conf.GetOpenedBoxes() {box.generateDocHistory0()}@@ -158,7 +155,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {}WaitForWritingFiles()- syncLock.Lock()+ writingDataLock.Lock()srcPath := historyPathvar destPath string@@ -169,22 +166,22 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {workingDoc := treenode.GetBlockTree(id)if nil != workingDoc {if err = os.RemoveAll(filepath.Join(util.DataDir, boxID, workingDoc.Path)); nil != err {- syncLock.Unlock()+ writingDataLock.Unlock()return}}destPath, err = getRollbackDockPath(boxID, historyPath)if nil != err {- syncLock.Unlock()+ writingDataLock.Unlock()return}if err = gulu.File.Copy(srcPath, destPath); nil != err {- syncLock.Unlock()+ writingDataLock.Unlock()return}- syncLock.Unlock()+ writingDataLock.Unlock()RefreshFileTree()IncWorkspaceDataVer()diff --git a/kernel/model/import.go b/kernel/model/import.goindex e8e25d092c..fa875265ce 100644--- a/kernel/model/import.go+++ b/kernel/model/import.go@@ -250,8 +250,8 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {}}- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()filesys.ReleaseAllFileLocks()@@ -279,7 +279,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {}IncWorkspaceDataVer()- refreshFileTree()+ RefreshFileTree()return}@@ -327,8 +327,8 @@ func ImportData(zipPath string) (err error) {return errors.New("write conf.json failed")}- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()filesys.ReleaseAllFileLocks()tmpDataPath := filepath.Dir(filepath.Dir(confPath))@@ -339,7 +339,7 @@ func ImportData(zipPath string) (err error) {}IncWorkspaceDataVer()- refreshFileTree()+ RefreshFileTree()return}@@ -347,8 +347,8 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {util.PushEndlessProgress(Conf.Language(73))WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()box := Conf.Box(boxID)var baseHPath, baseTargetPath, boxLocalPath string@@ -510,7 +510,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {}IncWorkspaceDataVer()- refreshFileTree()+ RefreshFileTree()} else { // 导入单个文件fileName := filepath.Base(localPath)if !strings.HasSuffix(fileName, ".md") && !strings.HasSuffix(fileName, ".markdown") {diff --git a/kernel/model/mount.go b/kernel/model/mount.goindex 72ea1dfa30..f0a5c5d9a5 100644--- a/kernel/model/mount.go+++ b/kernel/model/mount.go@@ -34,8 +34,8 @@ import (func CreateBox(name string) (id string, err error) {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()id = ast.NewNodeID()boxLocalPath := filepath.Join(util.DataDir, id)@@ -54,8 +54,8 @@ func CreateBox(name string) (id string, err error) {func RenameBox(boxID, name string) (err error) {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()box := Conf.Box(boxID)if nil == box {@@ -72,8 +72,8 @@ func RenameBox(boxID, name string) (err error) {func RemoveBox(boxID string) (err error) {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()if util.IsReservedFilename(boxID) {return errors.New(fmt.Sprintf("can not remove [%s] caused by it is a reserved file", boxID))@@ -115,8 +115,8 @@ func RemoveBox(boxID string) (err error) {func Unmount(boxID string) {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()unmount0(boxID)evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast, 0)@@ -141,8 +141,8 @@ func unmount0(boxID string) {func Mount(boxID string) (alreadyMount bool, err error) {WaitForWritingFiles()- syncLock.Lock()- defer syncLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()localPath := filepath.Join(util.DataDir, boxID)diff --git a/kernel/model/sync.go b/kernel/model/sync.goindex 53cae0c43b..f7ec9bcb44 100644--- a/kernel/model/sync.go+++ b/kernel/model/sync.go@@ -73,9 +73,6 @@ func SyncData(boot, exit, byHand bool) {return}- syncLock.Lock()- defer syncLock.Unlock()-if boot {util.IncBootProgress(3, "Syncing data from the cloud...")BootSyncSucc = 0@@ -127,8 +124,11 @@ func SyncData(boot, exit, byHand bool) {util.BroadcastByType("main", "syncing", 1, msg, nil)}()+ syncLock.Lock()+ defer syncLock.Unlock()+WaitForWritingFiles()- writingTreeLock.Lock()+ writingDataLock.Lock()var err error// 将 data 变更同步到 syncif err = workspaceData2SyncDir(); nil != err {@@ -141,7 +141,7 @@ func SyncData(boot, exit, byHand bool) {if exit {ExitSyncSucc = 1}- writingTreeLock.Unlock()+ writingDataLock.Unlock()return}@@ -156,10 +156,10 @@ func SyncData(boot, exit, byHand bool) {if exit {ExitSyncSucc = 1}- writingTreeLock.Unlock()+ writingDataLock.Unlock()return}- writingTreeLock.Unlock()+ writingDataLock.Unlock()cloudSyncVer, err := getCloudSyncVer(Conf.Sync.CloudName)if nil != err {diff --git a/kernel/model/transaction.go b/kernel/model/transaction.goindex 44d67fbbaa..8810139525 100644--- a/kernel/model/transaction.go+++ b/kernel/model/transaction.go@@ -43,7 +43,7 @@ var (ErrNotFullyBoot = errors.New("the kernel has not been fully booted, please try again later"))-var writingTreeLock = sync.Mutex{}+var writingDataLock = sync.Mutex{}func IsFoldHeading(transactions *[]*Transaction) bool {if 1 == len(*transactions) && 1 == len((*transactions)[0].DoOperations) {@@ -114,8 +114,8 @@ func AutoFlushTx() {}func flushTx() {- writingTreeLock.Lock()- defer writingTreeLock.Unlock()+ writingDataLock.Lock()+ defer writingDataLock.Unlock()defer util.Recover()currentTx = mergeTx()