Skip to content

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

:art: Concurrency control when requesting the kernel API https://github.com/siyuan-note/siyuan/issues/9939

Danielb5494e9af70a
diff --git a/kernel/model/session.go b/kernel/model/session.go
index e14a4452a0..75ca732d65 100644
--- a/kernel/model/session.go
+++ b/kernel/model/session.go
@@ -23,6 +23,7 @@ import (
"os"
"strconv"
"strings"
+ "sync"
"time"
"github.com/88250/gulu"
@@ -317,3 +318,22 @@ func Recover(c *gin.Context) {
c.Next()
}
+
+var (
+ requestingLock = sync.Mutex{}
+ requesting = map[string]*sync.Mutex{}
+)
+
+func ControlConcurrency(c *gin.Context) {
+ requestingLock.Lock()
+ mutex := requesting[c.Request.URL.Path]
+ if nil == mutex {
+ mutex = &sync.Mutex{}
+ requesting[c.Request.URL.Path] = mutex
+ }
+ requestingLock.Unlock()
+
+ mutex.Lock()
+ c.Next()
+ mutex.Unlock()
+}
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index 41c7ab4fd0..3a3698201b 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -52,6 +52,7 @@ func Serve(fastMode bool) {
ginServer := gin.New()
ginServer.MaxMultipartMemory = 1024 * 1024 * 32 // 插入较大的资源文件时内存占用较大 https://github.com/siyuan-note/siyuan/issues/5023
ginServer.Use(
+ model.ControlConcurrency, // 请求串行化 Concurrency control when requesting the kernel API https://github.com/siyuan-note/siyuan/issues/9939
model.Timing,
model.Recover,
corsMiddleware(), // 后端服务支持 CORS 预检请求验证 https://github.com/siyuan-note/siyuan/pull/5593