Skip to content

Commit

Permalink
Add Max Conn Idle Time
Browse files Browse the repository at this point in the history
  • Loading branch information
tung.tq committed Dec 20, 2023
1 parent b3bb374 commit a49e0e4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions config.tmp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mysql:
options: 'parseTime=true'
max_open_conns: 10
max_idle_conns: 5
max_conn_idle_time: 60m

client_type: redis
redis_num_servers: 2
Expand Down
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ type MySQLConfig struct {
Database string `mapstructure:"database"`
Options string `mapstructure:"options"`

MaxOpenConns uint32 `mapstructure:"max_open_conns"`
MaxIdleConns uint32 `mapstructure:"max_idle_conns"`
MaxOpenConns uint32 `mapstructure:"max_open_conns"`
MaxIdleConns uint32 `mapstructure:"max_idle_conns"`
MaxConnIdleTime time.Duration `mapstructure:"max_conn_idle_time"`
}

// RedisConfig ...
Expand Down
1 change: 1 addition & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mysql:
options: 'parseTime=true'
max_open_conns: 10
max_idle_conns: 5
max_conn_idle_time: 60m

client_type: redis
redis_num_servers: 2
Expand Down
18 changes: 10 additions & 8 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ func TestLoadConfig(t *testing.T) {

DBType: DBTypeMySQL,
MySQL: MySQLConfig{
Host: "localhost",
Port: 3306,
Username: "root",
Password: "1",
Database: "cache_inv",
Options: "parseTime=true",
MaxOpenConns: 10,
MaxIdleConns: 5,
Host: "localhost",
Port: 3306,
Username: "root",
Password: "1",
Database: "cache_inv",
Options: "parseTime=true",

MaxOpenConns: 10,
MaxIdleConns: 5,
MaxConnIdleTime: 60 * time.Minute,
},

ClientType: ClientTypeRedis,
Expand Down
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func initRepo(conf config.Config) cacheinv.Repository {
fmt.Println("Connect to MySQL:", conf.MySQL.PrintDSN())
fmt.Println("MySQL MaxOpenConns:", conf.MySQL.MaxOpenConns)
fmt.Println("MySQL MaxIdleConns:", conf.MySQL.MaxIdleConns)
fmt.Println("MySQL Max Conn Idle Time:", conf.MySQL.MaxConnIdleTime)

db := sqlx.MustOpen("mysql", conf.MySQL.DSN())
db.SetMaxOpenConns(int(conf.MySQL.MaxOpenConns))
db.SetMaxIdleConns(int(conf.MySQL.MaxIdleConns))
db.SetConnMaxIdleTime(conf.MySQL.MaxConnIdleTime)

fmt.Println("event_table_name:", conf.EventTableName)
fmt.Println("offset_table_name:", conf.OffsetTableName)
Expand Down

0 comments on commit a49e0e4

Please sign in to comment.