From 4781ee95f6439cdc3589e4a9f75d8b37629eba27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=BB=8B=E6=B6=A6?= <807046079@qq.com> Date: Sun, 17 Nov 2024 21:58:03 +0800 Subject: [PATCH] fix: resolve goroutine leak caused by `egroup.WithContext` The `egroup.WithContext` method initializes a goroutine pool of size 1000 every time it is called. Instead of reusing or cleaning up the pool after use, it creates a new one, leading to goroutine leaks. This issue caused excessive memory usage and eventually led to the service being killed. This commit replaces `egroup.WithContext` with `errgroup.Group`. --- api/v1/account.go | 8 ++++---- api/v1/user.go | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/api/v1/account.go b/api/v1/account.go index 85a20bc..3178e86 100644 --- a/api/v1/account.go +++ b/api/v1/account.go @@ -3,6 +3,7 @@ package v1 import ( "context" "errors" + "golang.org/x/sync/errgroup" "time" "github.com/ZiRunHua/LeapLedger/api/request" @@ -16,7 +17,6 @@ import ( userModel "github.com/ZiRunHua/LeapLedger/model/user" "github.com/ZiRunHua/LeapLedger/util/timeTool" "github.com/gin-gonic/gin" - "github.com/songzhibin97/gkit/egroup" "gorm.io/gorm" ) @@ -570,7 +570,7 @@ func (a *AccountApi) GetUserInfo(ctx *gin.Context) { return } accountUser, account, nowTime := contextFunc.GetAccountUser(ctx), contextFunc.GetAccount(ctx), contextFunc.GetNowTime(ctx) - group := egroup.WithContext(ctx) + var group errgroup.Group var todayTotal, monthTotal *global.IEStatisticWithTime var recentTrans *response.TransactionDetailList for _, infoType := range requestData.Types { @@ -990,9 +990,9 @@ func (a *AccountApi) GetInfo(ctx *gin.Context) { return nil } // process and response - var group *egroup.Group + var group *errgroup.Group if len(types) > 1 { - group = egroup.WithContext(ctx) + group = &errgroup.Group{} } for i := range types { t := types[i] diff --git a/api/v1/user.go b/api/v1/user.go index e5b81e8..1565d32 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -1,6 +1,7 @@ package v1 import ( + "golang.org/x/sync/errgroup" "time" "github.com/ZiRunHua/LeapLedger/api/request" @@ -17,7 +18,6 @@ import ( "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v5" "github.com/pkg/errors" - "github.com/songzhibin97/gkit/egroup" ) type UserApi struct { @@ -458,8 +458,7 @@ func (u *UserApi) Home(ctx *gin.Context) { if false == pass { return } - - group := egroup.WithContext(ctx) + var group errgroup.Group nowTime, timeLocation := account.GetNowTime(), account.GetTimeLocation() year, month, day := nowTime.Date() var todayData, yesterdayData, weekData, monthData, yearData response.TransactionStatistic