Skip to content

Commit

Permalink
fix: resolve goroutine leak caused by egroup.WithContext
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
小滋润 committed Nov 17, 2024
1 parent d178bf5 commit 4781ee9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
8 changes: 4 additions & 4 deletions api/v1/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"context"
"errors"
"golang.org/x/sync/errgroup"
"time"

"github.com/ZiRunHua/LeapLedger/api/request"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 2 additions & 3 deletions api/v1/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"golang.org/x/sync/errgroup"
"time"

"github.com/ZiRunHua/LeapLedger/api/request"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4781ee9

Please sign in to comment.