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

This commit is contained in:
Daniel 2023-12-22 10:00:40 +08:00
parent 3c52ad491e
commit b5494e9af7
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 21 additions and 0 deletions

View file

@ -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()
}

View file

@ -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