Skip to content

Commit

Permalink
🎨 细化云端同步锁提升稳定性 Fix siyuan-note#5887
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 authored and leolee9086 committed Oct 4, 2022
1 parent 7143ee9 commit 7d4036d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
6 changes: 6 additions & 0 deletions kernel/model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,12 @@ func loadNodesByMode(node *ast.Node, inputIndex, mode, size int, isDoc, isHeadin
}

func writeJSONQueue(tree *parse.Tree) (err error) {
writingDataLock.Lock()
if err = filesys.WriteTree(tree); nil != err {
writingDataLock.Unlock()
return
}
writingDataLock.Unlock()
sql.UpsertTreeQueue(tree)
return
}
Expand All @@ -931,9 +934,12 @@ func indexWriteJSONQueue(tree *parse.Tree) (err error) {
}

func renameWriteJSONQueue(tree *parse.Tree, oldHPath string) (err error) {
writingDataLock.Unlock()
if err = filesys.WriteTree(tree); nil != err {
writingDataLock.Unlock()
return
}
writingDataLock.Unlock()
sql.RenameTreeQueue(tree, oldHPath)
treenode.ReindexBlockTree(tree)
return
Expand Down
3 changes: 3 additions & 0 deletions kernel/model/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func SyncData(boot, exit, byHand bool) {
return
}

writingDataLock.Lock()
defer writingDataLock.Unlock()

if util.IsMutexLocked(&syncLock) {
logging.LogWarnf("sync is in progress")
planSyncAfter(30 * time.Second)
Expand Down
7 changes: 5 additions & 2 deletions kernel/model/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ func AutoFlushTx() {
}
}

var txLock = sync.Mutex{}

func flushTx() {
writingDataLock.Lock()
defer writingDataLock.Unlock()
txLock.Lock()
defer txLock.Unlock()

defer logging.Recover()

currentTx = mergeTx()
Expand Down
6 changes: 6 additions & 0 deletions kernel/model/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
)

func InsertLocalAssets(id string, assetPaths []string) (succMap map[string]interface{}, err error) {
writingDataLock.Lock()
defer writingDataLock.Unlock()

succMap = map[string]interface{}{}

bt := treenode.GetBlockTree(id)
Expand Down Expand Up @@ -101,6 +104,9 @@ func Upload(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(200, ret)

writingDataLock.Lock()
defer writingDataLock.Unlock()

form, err := c.MultipartForm()
if nil != err {
logging.LogErrorf("insert asset failed: %s", err)
Expand Down

0 comments on commit 7d4036d

Please sign in to comment.