ソースを参照

:art: Prevent frontend request pending https://github.com/siyuan-note/siyuan/issues/10992

Daniel 1 年間 前
コミット
ee473289d2
3 ファイル変更32 行追加23 行削除
  1. 18 9
      kernel/sql/queue.go
  2. 10 10
      kernel/sql/queue_asset_content.go
  3. 4 4
      kernel/sql/queue_history.go

+ 18 - 9
kernel/sql/queue.go

@@ -35,6 +35,7 @@ import (
 var (
 	operationQueue []*dbQueueOperation
 	dbQueueLock    = sync.Mutex{}
+	txLock         = sync.Mutex{}
 )
 
 type dbQueueOperation struct {
@@ -95,29 +96,30 @@ func ClearQueue() {
 }
 
 func FlushQueue() {
-	dbQueueLock.Lock()
-	defer dbQueueLock.Unlock()
-
-	total := len(operationQueue)
+	ops := getOperations()
+	total := len(ops)
 	if 1 > total {
 		return
 	}
 
+	txLock.Lock()
+	defer txLock.Unlock()
+
 	start := time.Now()
 
 	context := map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}
-	if 512 < total {
+	if 512 < len(ops) {
 		disableCache()
 		defer enableCache()
 	}
 
 	groupOpsTotal := map[string]int{}
-	for _, op := range operationQueue {
+	for _, op := range ops {
 		groupOpsTotal[op.action]++
 	}
 
 	groupOpsCurrent := map[string]int{}
-	for i, op := range operationQueue {
+	for i, op := range ops {
 		if util.IsExiting.Load() {
 			return
 		}
@@ -150,8 +152,6 @@ func FlushQueue() {
 		debug.FreeOSMemory()
 	}
 
-	operationQueue = nil
-
 	elapsed := time.Now().Sub(start).Milliseconds()
 	if 7000 < elapsed {
 		logging.LogInfof("database op tx [%dms]", elapsed)
@@ -418,3 +418,12 @@ func RemoveTreePathQueue(treeBox, treePathPrefix string) {
 	}
 	operationQueue = append(operationQueue, newOp)
 }
+
+func getOperations() (ops []*dbQueueOperation) {
+	dbQueueLock.Lock()
+	defer dbQueueLock.Unlock()
+
+	ops = operationQueue
+	operationQueue = nil
+	return
+}

+ 10 - 10
kernel/sql/queue_asset_content.go

@@ -33,8 +33,7 @@ import (
 var (
 	assetContentOperationQueue []*assetContentDBQueueOperation
 	assetContentDBQueueLock    = sync.Mutex{}
-
-	assetContentTxLock = sync.Mutex{}
+	assetContentTxLock         = sync.Mutex{}
 )
 
 type assetContentDBQueueOperation struct {
@@ -51,7 +50,8 @@ func FlushAssetContentTxJob() {
 
 func FlushAssetContentQueue() {
 	ops := getAssetContentOperations()
-	if 1 > len(ops) {
+	total := len(ops)
+	if 1 > total {
 		return
 	}
 
@@ -97,7 +97,7 @@ func FlushAssetContentQueue() {
 		}
 	}
 
-	if 128 < len(ops) {
+	if 128 < total {
 		debug.FreeOSMemory()
 	}
 
@@ -122,24 +122,24 @@ func execAssetContentOp(op *assetContentDBQueueOperation, tx *sql.Tx, context ma
 }
 
 func DeleteAssetContentsByPathQueue(path string) {
-	assetContentTxLock.Lock()
-	defer assetContentTxLock.Unlock()
+	assetContentDBQueueLock.Lock()
+	defer assetContentDBQueueLock.Unlock()
 
 	newOp := &assetContentDBQueueOperation{inQueueTime: time.Now(), action: "deletePath", path: path}
 	assetContentOperationQueue = append(assetContentOperationQueue, newOp)
 }
 
 func IndexAssetContentsQueue(assetContents []*AssetContent) {
-	assetContentTxLock.Lock()
-	defer assetContentTxLock.Unlock()
+	assetContentDBQueueLock.Lock()
+	defer assetContentDBQueueLock.Unlock()
 
 	newOp := &assetContentDBQueueOperation{inQueueTime: time.Now(), action: "index", assetContents: assetContents}
 	assetContentOperationQueue = append(assetContentOperationQueue, newOp)
 }
 
 func getAssetContentOperations() (ops []*assetContentDBQueueOperation) {
-	assetContentTxLock.Lock()
-	defer assetContentTxLock.Unlock()
+	assetContentDBQueueLock.Lock()
+	defer assetContentDBQueueLock.Unlock()
 
 	ops = assetContentOperationQueue
 	assetContentOperationQueue = nil

+ 4 - 4
kernel/sql/queue_history.go

@@ -33,8 +33,7 @@ import (
 var (
 	historyOperationQueue []*historyDBQueueOperation
 	historyDBQueueLock    = sync.Mutex{}
-
-	historyTxLock = sync.Mutex{}
+	historyTxLock         = sync.Mutex{}
 )
 
 type historyDBQueueOperation struct {
@@ -51,7 +50,8 @@ func FlushHistoryTxJob() {
 
 func FlushHistoryQueue() {
 	ops := getHistoryOperations()
-	if 1 > len(ops) {
+	total := len(ops)
+	if 1 > total {
 		return
 	}
 
@@ -97,7 +97,7 @@ func FlushHistoryQueue() {
 		}
 	}
 
-	if 128 < len(ops) {
+	if 128 < total {
 		debug.FreeOSMemory()
 	}