Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

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

Danielee473289d247
diff --git a/kernel/sql/queue.go b/kernel/sql/queue.go
index ad22870df3..289979e09f 100644
--- a/kernel/sql/queue.go
+++ b/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
+}
diff --git a/kernel/sql/queue_asset_content.go b/kernel/sql/queue_asset_content.go
index 08b7c839e5..67eb955dd3 100644
--- a/kernel/sql/queue_asset_content.go
+++ b/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
diff --git a/kernel/sql/queue_history.go b/kernel/sql/queue_history.go
index a0338e0b9a..418a821600 100644
--- a/kernel/sql/queue_history.go
+++ b/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()
}