From 18c033d57f433e3054e7253ed4c27c28d9287939 Mon Sep 17 00:00:00 2001 From: RockYang Date: Fri, 8 Sep 2023 17:23:32 +0800 Subject: [PATCH] opt: add lock for mj task callback --- api/handler/mj_handler.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/api/handler/mj_handler.go b/api/handler/mj_handler.go index 522008b..82550be 100644 --- a/api/handler/mj_handler.go +++ b/api/handler/mj_handler.go @@ -13,6 +13,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "gorm.io/gorm" + "sync" "time" ) @@ -41,6 +42,7 @@ type MidJourneyHandler struct { db *gorm.DB mjFunc function.FuncMidJourney uploaderManager *oss.UploaderManager + lock sync.Mutex } func NewMidJourneyHandler( @@ -53,6 +55,7 @@ func NewMidJourneyHandler( leveldb: leveldb, db: db, uploaderManager: manager, + lock: sync.Mutex{}, mjFunc: functions[types.FuncMidJourney].(function.FuncMidJourney)} h.App = app return &h @@ -80,7 +83,9 @@ func (h *MidJourneyHandler) Notify(c *gin.Context) { } logger.Debugf("收到 MidJourney 回调请求:%+v", data) - + h.lock.Lock() + defer h.lock.Unlock() + // the job is saved var job model.MidJourneyJob res := h.db.Where("message_id = ?", data.MessageId).First(&job)