go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

Memory leak occurred,When loading the yaml configuration file

Open xiachuanqi555 opened this issue 3 months ago • 2 comments

Describe the bug A clear and concise description of what the bug is.

I configured the mysql conf in the yaml file, but I introduced the slave in its structure An optional tag was added to distinguish the master from the slave. When loading the yaml configuration file, when reflecting and parsing the configuration of mysql and attempting to parse the structure of mysql, it would always enter the parsing of the slave. The optional did not work and fell into an infinite loop, resulting in a memory leak

To Reproduce Steps to reproduce the behavior, if applicable:

  1. The code is

type GlobalConfig struct { CN CountryConfig }

type CountryConfig struct { MySQL MySQLConfig json:",optional" }

type MySQLConfig struct { Alias string json:",optional" DSN string json:",optional" Type string json:",optional" // master or slave MaxOpenConns int json:",optional" MaxIdleConns int json:",optional" ConnMaxIdleTime time.Duration json:",optional" ConnMaxLifetime time.Duration json:",optional" LogMode logger.LogLevel json:",optional" SlowThreshold time.Duration json:",optional" Slave []*MySQLConfig json:",optional" }


2. The error is

no error, but mem leak


**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Environments (please complete the following information):**
- OS: [mac os]
- go-zero version [1.9.0]

**More description**
Add any other context about the problem here.

xiachuanqi555 avatar Sep 03 '25 09:09 xiachuanqi555

My suggestion is..


type MySQLConfigCore struct {
    Alias string json:",optional"
    DSN string json:",optional"
    Type string json:",optional" // master or slave
    MaxOpenConns int json:",optional"
    MaxIdleConns int json:",optional"
    ConnMaxIdleTime time.Duration json:",optional"
    ConnMaxLifetime time.Duration json:",optional"
    LogMode logger.LogLevel json:",optional"
    SlowThreshold time.Duration json:",optional"
}
type MySQLConfig struct {
    MySQLConfigCore
    Slave []*MySQLConfigCore json:",optional"
}

guonaihong avatar Sep 03 '25 13:09 guonaihong

type MySQLNodeConf struct {
    Alias string json:",optional"
    DSN string json:",optional"
    Type string json:",optional" // master or slave
    MaxOpenConns int json:",optional"
    MaxIdleConns int json:",optional"
    ConnMaxIdleTime time.Duration json:",optional"
    ConnMaxLifetime time.Duration json:",optional"
    LogMode logger.LogLevel json:",optional"
    SlowThreshold time.Duration json:",optional"
}
type MySQLConfig struct {
    Master MySQLNodeConf
    Slaves []*MySQLNodeConf json:",optional"
}

kevwan avatar Sep 07 '25 07:09 kevwan