fix: 在 PullCodeHandler、BuildBotHandler 和 RestartBotHandler 中使用 TryLock 以避免并发请求导致的错误响应

This commit is contained in:
lixiangwuxian 2025-05-03 23:38:34 +08:00
parent d4038a568e
commit 8759a2ce65

View File

@ -173,7 +173,10 @@ func buildBot() error {
} }
func PullCodeHandler(c *gin.Context) { func PullCodeHandler(c *gin.Context) {
stuffMutex.Lock() if !stuffMutex.TryLock() {
c.JSON(http.StatusInternalServerError, gin.H{"error": "有其他请求正在运行,请稍后再试"})
return
}
defer stuffMutex.Unlock() defer stuffMutex.Unlock()
err := util.GitPull() err := util.GitPull()
log.Println("拉取代码...") log.Println("拉取代码...")
@ -186,7 +189,10 @@ func PullCodeHandler(c *gin.Context) {
} }
func BuildBotHandler(c *gin.Context) { func BuildBotHandler(c *gin.Context) {
stuffMutex.Lock() if !stuffMutex.TryLock() {
c.JSON(http.StatusInternalServerError, gin.H{"error": "有其他请求正在运行,请稍后再试"})
return
}
defer stuffMutex.Unlock() defer stuffMutex.Unlock()
err := buildBot() err := buildBot()
log.Println("构建程序...") log.Println("构建程序...")
@ -204,7 +210,10 @@ func RestartBotHandler(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return return
} }
stuffMutex.Lock() if !stuffMutex.TryLock() {
c.JSON(http.StatusInternalServerError, gin.H{"error": "有其他请求正在运行,请稍后再试"})
return
}
defer stuffMutex.Unlock() defer stuffMutex.Unlock()
err := restartProgram() err := restartProgram()
log.Println("重启程序...") log.Println("重启程序...")