From aebbc4b889a9445aa7efbcafdc551b016ab68cf6 Mon Sep 17 00:00:00 2001 From: Sean Du Date: Mon, 13 Jul 2026 07:53:43 +0800 Subject: [PATCH] Abort requests when session transactions cannot start Signed-off-by: Sean Du --- internal/handler/ghome/account/active.go | 3 +++ internal/handler/ghome/account/initialize.go | 3 +++ internal/handler/ghome/account/login.go | 3 +++ internal/handler/ghome/account/loginauto.go | 3 +++ internal/handler/ghome/account/reportrole.go | 3 +++ internal/handler/ghome/basic/getcode.go | 3 +++ internal/handler/ghome/basic/getproductlist.go | 3 +++ internal/handler/ghome/basic/handshake.go | 3 +++ internal/handler/ghome/basic/loginarea.go | 3 +++ internal/handler/ghome/basic/publickey.go | 3 +++ internal/handler/ghome/guest/status.go | 3 +++ internal/handler/ghome/misc/agreement.go | 3 +++ internal/handler/ghome/misc/integration.go | 3 +++ internal/session/session.go | 4 +++- 14 files changed, 42 insertions(+), 1 deletion(-) diff --git a/internal/handler/ghome/account/active.go b/internal/handler/ghome/account/active.go index 865ddb9..430abf2 100644 --- a/internal/handler/ghome/account/active.go +++ b/internal/handler/ghome/account/active.go @@ -10,6 +10,9 @@ import ( func active(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() ss.Respond(ghomeschema.ActiveResp{ diff --git a/internal/handler/ghome/account/initialize.go b/internal/handler/ghome/account/initialize.go index ba73074..3b65337 100644 --- a/internal/handler/ghome/account/initialize.go +++ b/internal/handler/ghome/account/initialize.go @@ -14,6 +14,9 @@ import ( func initialize(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() initData := ghomeschema.InitializeData{ diff --git a/internal/handler/ghome/account/login.go b/internal/handler/ghome/account/login.go index c391b23..605af2c 100644 --- a/internal/handler/ghome/account/login.go +++ b/internal/handler/ghome/account/login.go @@ -34,6 +34,9 @@ const ( func login(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() data, err := ctx.GetRawData() diff --git a/internal/handler/ghome/account/loginauto.go b/internal/handler/ghome/account/loginauto.go index b7e4085..51bfb89 100644 --- a/internal/handler/ghome/account/loginauto.go +++ b/internal/handler/ghome/account/loginauto.go @@ -16,6 +16,9 @@ import ( func loginAuto(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() data, err := ctx.GetRawData() diff --git a/internal/handler/ghome/account/reportrole.go b/internal/handler/ghome/account/reportrole.go index 37c0c18..9a0a68e 100644 --- a/internal/handler/ghome/account/reportrole.go +++ b/internal/handler/ghome/account/reportrole.go @@ -12,6 +12,9 @@ import ( func reportRole(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() randKey, err := ss.Get3DESRandKey() diff --git a/internal/handler/ghome/basic/getcode.go b/internal/handler/ghome/basic/getcode.go index 2743946..e8580bc 100644 --- a/internal/handler/ghome/basic/getcode.go +++ b/internal/handler/ghome/basic/getcode.go @@ -11,6 +11,9 @@ import ( func getCode(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() codeArray := `{"codeArray":[{"btntext":"好的","code":"-10264022","msg_from":2,"text":"","title":"短信验证码被阻止","type":1},{"btntext":"","code":"-10869623","msg_from":2,"text":"","title":"网络连接失败,无法一键登录","type":2},{"btntext":"","code":"10298300","msg_from":2,"text":"","title":"","type":3},{"btntext":"","code":"10298311","msg_from":2,"text":"","title":"","type":3},{"btntext":"","code":"10298312","msg_from":2,"text":"","title":"","type":3},{"btntext":"","code":"10298313","msg_from":2,"text":"","title":"","type":1},{"btntext":"","code":"10298321","msg_from":2,"text":"","title":"","type":3},{"btntext":"","code":"10298322","msg_from":2,"text":"","title":"","type":3}],"codeVersion":"1.0.5"}` diff --git a/internal/handler/ghome/basic/getproductlist.go b/internal/handler/ghome/basic/getproductlist.go index d57731b..c4ec4be 100644 --- a/internal/handler/ghome/basic/getproductlist.go +++ b/internal/handler/ghome/basic/getproductlist.go @@ -10,6 +10,9 @@ import ( func getProductList(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() getProductListData := ghomeschema.GetProductListData{ diff --git a/internal/handler/ghome/basic/handshake.go b/internal/handler/ghome/basic/handshake.go index ef5781e..46fea29 100644 --- a/internal/handler/ghome/basic/handshake.go +++ b/internal/handler/ghome/basic/handshake.go @@ -18,6 +18,9 @@ import ( func handshake(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() data, err := ctx.GetRawData() diff --git a/internal/handler/ghome/basic/loginarea.go b/internal/handler/ghome/basic/loginarea.go index a60a620..4a92ed9 100644 --- a/internal/handler/ghome/basic/loginarea.go +++ b/internal/handler/ghome/basic/loginarea.go @@ -10,6 +10,9 @@ import ( func loginArea(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() ss.Respond(ghomeschema.LoginAreaResp{ diff --git a/internal/handler/ghome/basic/publickey.go b/internal/handler/ghome/basic/publickey.go index 6c300a8..abfcd2a 100644 --- a/internal/handler/ghome/basic/publickey.go +++ b/internal/handler/ghome/basic/publickey.go @@ -14,6 +14,9 @@ import ( func publicKey(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() publicKeyCode := 0 diff --git a/internal/handler/ghome/guest/status.go b/internal/handler/ghome/guest/status.go index 1987429..38bd0c6 100644 --- a/internal/handler/ghome/guest/status.go +++ b/internal/handler/ghome/guest/status.go @@ -10,6 +10,9 @@ import ( func status(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() ss.Respond(ghomeschema.GuestStatusResp{ diff --git a/internal/handler/ghome/misc/agreement.go b/internal/handler/ghome/misc/agreement.go index 72fd387..ba8359e 100644 --- a/internal/handler/ghome/misc/agreement.go +++ b/internal/handler/ghome/misc/agreement.go @@ -10,6 +10,9 @@ import ( func agreement(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() ss.Respond(ghomeschema.AgreementResp{ diff --git a/internal/handler/ghome/misc/integration.go b/internal/handler/ghome/misc/integration.go index 77f99d1..cd286d9 100644 --- a/internal/handler/ghome/misc/integration.go +++ b/internal/handler/ghome/misc/integration.go @@ -10,6 +10,9 @@ import ( func appReport(ctx *gin.Context) { ss := session.Attach(ctx) + if ss.Done() { + return + } defer ss.FinalizeOrRollback() ss.Respond(ghomeschema.AppReportResp{ diff --git a/internal/session/session.go b/internal/session/session.go index 00a7525..b6ed121 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -51,7 +51,9 @@ func New(ctx *gin.Context) *Session { ss.MainEng = db.MainEng.NewSession() ss.UserEng = db.UserEng.NewSession() - ss.UserEng.Begin() + if err := ss.UserEng.Begin(); err != nil { + ss.Abort(err) + } return ss }